-
-
Notifications
You must be signed in to change notification settings - Fork 373
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
Support for annotations (PEP 484 type hints) #97
Comments
My thoughts are that that makes sense to have and shouldn’t be too hard to achieve. The question is how to integrate it. We could pull the type information from the instance_of validator or bite the bullet and introduce a full-fledged on type field that would also help with the deserialization efforts. |
I'll have to think a bit about those options. It seems really strange for attr.ib "core" to pull info like that out of a special validator (it's like adding some kind of implicit side-channel to the validator API that only this one special validator gets to use). Also, PEP 484 type hints aren't necessarily expressed the same as runtime types (e.g. for optional types). On the other hand, it's obviously closely related, and would be ugly to have to specify both, especially when they are the same. The other problem with this whole idea is that PEP 484 hints are mostly for static analysis, and having annotations in import-time generated code isn't going to help those analyzers at all :-) So even with support in attrs to make these annotations available at runtime, making use of them with a type analyzer would require something else (an extension to the static analyzer to understand |
Another thought to consider. In 3.6, this will be possible IIUC:
I think it would be a great waste not to expose this type information somehow. Probably make In any case, once we have this info, it's not hard to add the annotations to the generated init if we want. |
Before Python 3.6 annotating attributes was also possible; it is in PEP 484: @attr.s
class A:
a = attr.ib(default=None) # type: int PyCharm seems to understand such annotations; A().a inferred type is But none of PyCharm or mypy understand See also: python/mypy#1310. |
Actually in PyCharm |
@Tinche yeah, you're right! It shows |
I believe this is the |
Closing in favor of #214 |
Hi Hynek!
I'd like the generated
__init__
signature to include type annotations in Python 3.Somebody mentioned this in passing over in #63, but after reading through that ticket and glancing at the PR, I'm still not sure how that feature would let me actually get type annotations into the generated
__init__
signature. It seems like this requires dedicated support in attrs, probably as a separate kwarg toattr.ib
.Any thoughts?
The text was updated successfully, but these errors were encountered: