Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Python type hints for UML generation #1548

Closed
lordascor opened this issue Jun 22, 2017 · 9 comments · Fixed by #4551
Closed

Use Python type hints for UML generation #1548

lordascor opened this issue Jun 22, 2017 · 9 comments · Fixed by #4551
Labels
Enhancement ✨ Improvement to a component Good first issue Friendly and approachable by new contributors Help wanted 🙏 Outside help would be appreciated, good for new contributors Minor 💅 Polishing pylint is always nice pyreverse Related to pyreverse component

Comments

@lordascor
Copy link

It seems that pyreverse does not read python type hints (as defined by PEP 484), and this does not help when you use None as a default value :

Code example

class C(object):
    def __init__(self, a: str = None):
        self.a = a

Current behavior

Output of pyreverse :

classes_test

Expected behavior

I would like to see something like : a : String in the output.

pylint --version output

pylint-script.py 1.6.5,
astroid 1.4.9
Python 3.6.0 |Anaconda custom (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1900 64 bit (AMD64)]

@rogalski rogalski added the Enhancement ✨ Improvement to a component label Jun 22, 2017
@PCManticore PCManticore added the Minor 💅 Polishing pylint is always nice label Jun 26, 2017
@AWhetter AWhetter added the pyreverse Related to pyreverse component label Jul 8, 2017
@pohutukawa
Copy link

Is this something that's in the pipeline (or going to make it)? Type hinting is becoming more and more common, and this will be a huge benefit towards working with pyreverse. Especially as it's considered Python idiomatic (and sensible) to use None as a default parameter for mutable data structures.

@PCManticore
Copy link
Contributor

@pohutukawa We don't have a pipeline per se, but the one we have is represented by the milestones and the issues we assign to each milestone. Regarding this one, it's not currently in pipeline and most likely it's not going to be too soon, mostly due to limited resources on our end (e.g. I can only focus on maybe 1, 2 issues per day).

@pohutukawa
Copy link

@PCManticore Thanks for the heads up. That's OK, just thought to ask as the ticket's already almost a year and a half old. Nice to have, but understandable.
Keep chipping away, and good job on the tools provided in the first place!

@labrys
Copy link

labrys commented Apr 16, 2020

I understand your resources are limited, so I understand if you can't add this to the milestone just yet.

I think that with type-hints becoming more important with each Python release (e.g. typing.NamedTuple in Python 3.6, dataclasses.Dataclass in Python 3.7, typing.TypedDict in Python 3.8, etc) this would be a phenomenally useful addition to pyreverse if you get a chance to implement it.

@PCManticore PCManticore added the Good first issue Friendly and approachable by new contributors label Apr 21, 2020
@khimaros
Copy link

@PCManticore -- if someone were interested in helping with this issue, what would be the best way to engage? Do you have any design guidance or recommendations?

@Pierre-Sassoulas Pierre-Sassoulas added the Help wanted 🙏 Outside help would be appreciated, good for new contributors label Mar 2, 2021
mbyrnepr2 added a commit to mbyrnepr2/pylint that referenced this issue Jun 5, 2021
mbyrnepr2 added a commit to mbyrnepr2/pylint that referenced this issue Jun 7, 2021
mbyrnepr2 added a commit to mbyrnepr2/pylint that referenced this issue Jun 9, 2021
mbyrnepr2 added a commit to mbyrnepr2/pylint that referenced this issue Jun 9, 2021
mbyrnepr2 added a commit to mbyrnepr2/pylint that referenced this issue Jun 9, 2021
mbyrnepr2 added a commit to mbyrnepr2/pylint that referenced this issue Jun 10, 2021
@penguinolog
Copy link

Looks like annotation is incorrect anyway. It should be Optional[str] or str|None for the newest pythons (3.10+). From MyPy side example contains error.

@Pierre-Sassoulas
Copy link
Member

@penguinolog is right about the problem in the example. @khimaros one could start by reading code in pyreverse, here's a merge request modifying something in pyreverse recently : #4521

Pierre-Sassoulas pushed a commit that referenced this issue Jun 26, 2021
* Add type annotations to pyreverse dot files

Closes #1548

- Indicate the attribute is optional in the dot files by
  inspecting the default value.
- Handle the Subscript type annotations
- Refactor & move the logic to obtain annotations to utils.py as
  re-usable functions
- Add unittests for the added functions
- Add a try/except to deal with a possible InferenceError when using
NodeNG.infer method
- Create a function in utils and so remove repeated logic in inspector.py
- Add unittests to check the InferenceError logic
- Adjust the types in function input
@BenBlumer
Copy link

Thanks for your work on this @mbyrnepr2 .

Quick question: It looks like the use case described in the first post works, but it doesn't show the "has-a" relationship the same way it would for a class instance defined within.

For example:

Here, where the instance of class B is passed to A's initializer, A's "box" shows it has a member of type B, but doesn't show this graphically.
image

Here, where an instance of B is created in class A's initializer, it does show it graphically.
image

Is there a way have this visual "has a" relationship to work for type-hinted, passed, arguments?

@mbyrnepr2
Copy link
Member

Hi @BenBlumer thanks pointing this out. I've created an issue for it from the details in your report above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Good first issue Friendly and approachable by new contributors Help wanted 🙏 Outside help would be appreciated, good for new contributors Minor 💅 Polishing pylint is always nice pyreverse Related to pyreverse component
Projects
None yet
Development

Successfully merging a pull request may close this issue.