How does intellisense infer the __init__ method from dataclass annotation? #24134
Replies: 2 comments
-
|
Oh is this the key? https://github.com/python/mypy/blob/4554bd0c6d49506a4a1c77dcf344dca4efee92e1/mypy/plugins/dataclasses.py#L204 |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Was able to unblock myself in |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey everyone! Not sure where the best place was to answer this question but was hoping this was a good place to start.
I'm interested in understanding how IDEs, mypy, etc understand that a class annotated with
@dataclassis able to infer what the type signature of the__init__method of that new class is. In other words, how the signature on that appears on hover is generated (see below):I understand how it works at runtime. From reading the source code, I see how the annotation creates an
__init__methods usingexecand then attaches to the annotated class as an attribute. However, my interest is on the static analysis side. What is the key piece that allows other systems to know the type hints of__init__dynamically?I created a repro repo where I'm interested in replicating this behavior for another annotation. In that example, I tried copy pasting the exact source code and typeshed annotations for
dataclasses.py, resolved all mypy errors that arose, switched the name fromdataclasstovargas, and saw that I was still unable to get the desired end effect, as evidenced my thetype: ignoreneeded next to the initialization ofB. My end goal is discovering how to make an annotation that could generate type hints for any attribute, using whatever magic dataclass's are using under the hood.Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions