-
-
Notifications
You must be signed in to change notification settings - Fork 383
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
don't use __annotations__ #288
Comments
I’m very confused. They want to remove Why am I already regretting ever supporting this half-baked bullshit? :'( |
Random observation while working on #292: |
Wait, what? Where do they want to remove |
@hynek I've been following that thread and the Python-ideas one, too. I don't think the removal's gonna happen, though at this stage it was certainly disconcerting, even as just a suggestion. |
Looks like I can only lose here. Yay. |
Maybe the right thing to do is simply to be aware of this impending change and add version-specific Python support for parsing / evaluating the annotation strings in the future. It does look like it going to have a fairly precise contract. For that matter, annotations can be strings today; a correct implementation would handle any declaration as a possible forward declaration. For example,
is a perfectly valid type declaration today, and arguably So perhaps we should just focus on handling forward declarations on current python and then let this shake out naturally in the future version? |
Taking that thought further: is this just a dup of #265 ? |
Hi. So I've been trying to implement some code that uses the annotation for type checking. And I've run into several interesting issues with The
Example:
But giving it globals and locals you'd be ok:
However because of forward and self references you can't guarantee that at class creation type (when attrs is checking for this stuff)
So if the goal is to always get "honest to goodness types" the only way to do it is to save the stack frame in which the class is created, wait until an instance is about to be instantiated, call I've got some hacky code that does that if you want to see it. |
Thanks for the writeup! Happy to see your code! As for the status quo: I think it’s fair game to say that we don’t resolve the types for now however maybe we should document it. |
Here's another interesting find. python/cpython@f350a26 This would help with classes declared at module scope but not with classes declared in functions and such. Let me see if I can whip my code into shape. |
This is being somewhat resolved by PEP 563. |
So I played around with this for a while and came to the following conclusion. attrs should probably use The real issue is there's no way to guarantee that I found three places where you can call
Here's the code to resolve_types: I believe caching is useful because typing.get_type_hints can be somewhat slow. I don't know how y'all feel about hanging something off the class.
This was how I modified _get_annotations to get them at class creation time:
And this is how I check them at instance creation time, by adding it to the generated init.
|
I put this code into #302 |
So um could someone give me an executive summary of all of this? :). Assuming #302 is merged, is there any other immediate need to do something? FTR, data classes use |
To recap: The
Problem 1 Class GenerationThis is similar to ericvsmith/dataclasses#92 The current class generating code only cares if the type is a Problem 2 The
|
IOW, we leave this be until the dust in #302 settles, right? |
I think so. |
If I understand the way #302 was closed correctly, we should tackle this now, right? |
I believe the only thing we must do is make sure ClassVar works correctly. The other part is slightly ambiguous. Do we care what gets put into
and attrs itself wouldn't care. If we don't care then we can do nothing. Caring is hard because python/typing#508 is not fixed yet and so you can't safely call |
Hmm. Actually let me play around with how Python 3.7 will actually work with |
Yeah but you're not wrong that 508 needs addressing before 3.7 is released. |
❤️❤️❤️ |
Don't slow down attrs by checking if type= is a type. |
No open source maintenance best open source maintenance. |
Just to circle back to this:
I does work correctly, right? So what exactly is the plan for this issue? Anything left to be done or are we gonna keep using |
I filed the current ClassVar issue as #361 |
Just found this, which seems to break the for now, can we at least update the documentation in either http://www.attrs.org/en/stable/api.html#attr.ib or http://www.attrs.org/en/stable/api.html#attr.Attribute to mention that It took me a while to figure out what was going on and PEP-563 will be landing in a future version of python, so I think it would be valuable to at least have a note about it |
Bumping this because we are several python releases closer to this change breaking everything:) |
Well it won't break everything. IIUC the only thing that happens is that That being said 3rd party tools that might want to access the |
I mainly suggested that the docs be updated to have a warning/info box
about this
…On Mon, Feb 17, 2020, 20:53 David Euresti ***@***.***> wrote:
Well it won't break everything. IIUC the only thing that happens is that
.type would now return a string. Nothing in attrs itself will be damaged
by this. We already handle ClassVar in a decent way. (Though not as
hard-core as dataclasses does python/cpython#6768
<python/cpython#6768>)
That being said 3rd party tools that might want to access the type
attribute will start seeing strings and that might confuse them. If we
think it's important to support "dereferencing .type then we could bring
back #302 <#302> though this
could easily be done by the 3rd party tool that needs it.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#288?email_source=notifications&email_token=AAGD7YWHRPV5R2PLUH5PRFDRDNLSHA5CNFSM4EDCYFBKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMAPPKI#issuecomment-587265961>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAGD7YXE6C2MLJJC6LP32G3RDNLSHANCNFSM4EDCYFBA>
.
|
So I guess I should add support to cattrs for this? I wonder if there's comprehensive docs anywhere for tools that use runtime types that would help me adjust. Are we just supposed to call get_type_hints() if we encounter a string type? Is that going to be super slow if we do it on every single use? TBH I feel like the Python typing crowd kinda looks down on using types in runtime, which is amusing because runtime types are several orders of magnitude more useful to me at this point than running mypy over my codebases (which remains borderline useless). My codebases simply wouldn't work at all without runtime types, and the alternatives are much, much uglier and less user-friendly. |
I think the right move would indeed be to bring back #302 and add an option to do it automatically? |
So the issue is that it can't be done automatically because the types might not be there if there are future references. For example
If you try to resolve types when Foo is created you'd fail because Bar doesn't exist. You need to resolve the types for Foo after Bar has been created. |
Bleh oh well. Does anything speak against reviving #302? |
Heyyy , #302 has been merged! Any objections against closing this? |
Sorry I didn't comment, but no objections, it looks good, I'll test it out later once I have a free moment |
attrs/src/attr/_make.py
Line 213 in aa50117
__annotations__
However, it looks as though an upcoming incompatible change to Python will change
__annotations__
into a list of strings.It seems like the correct public API to use to retrieve the information
attrs
wants would beget_type_hints
.As a bonus, fixing this might make #265 easier to implement.
The text was updated successfully, but these errors were encountered: