Handle type[...] same as typing.Type[...] in classmethod 1st arg#15297
Handle type[...] same as typing.Type[...] in classmethod 1st arg#15297hauntsaninja merged 5 commits intopython:masterfrom
Conversation
|
That seems like a lot of duplication; maybe we should add a helper to return whether something is valid as Also, please add a new test. |
This comment has been minimized.
This comment has been minimized.
|
@JelleZijlstra Thanks for taking a look! I tried do deduplicate a little but I'm not sure if I like this better and / or if this is what you meant. Will look at adding a test once the actual code seems satisfactory. |
This comment has been minimized.
This comment has been minimized.
|
Yeah, on second thought, I am not happy with my initial attempt at deduplication, and I think I should revert it. I don't think that I should be adding any surface area to the semantic analyzer API just to abstract away a few common patterns (they're not even duplications, really). Trying to find a better way to do it / place to put it, I keep running into things that seem to me to be incomplete, e.g. the map of type aliases and its inverse in nodes.py. I guess I still don't grok this codebase sufficiently... I will try to find some time to look into this a bit more, probably starting with a bunch of tests based off the specification of the PEP itself. But for now I would propose going back to my initial commit, which fixes quite precisely the bug I encountered and nothing else. Does that make sense? |
|
I have added a bunch of tests, but perhaps I went I bit overboard. I made a new Just to be clear, not all of these new tests were failing before the change I am proposing to make here in PS Apologies for the force-push, I realize that isn't always convenient for reviewers... |
This comment has been minimized.
This comment has been minimized.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
hauntsaninja
left a comment
There was a problem hiding this comment.
The change itself looks great, thanks for fixing this!
I agree that the tests are a little overzealous and your suggestion of pruning down would be good to do.
|
@hauntsaninja Thanks for reviewing! Yes, indeed, I went a bit overboard with tests -- I just wanted to be quite sure I wasn't breaking anything. I've removed all but the two tests that were actually failing without this proposed patch to |
|
PS I just did the same overzealous-test-reduction for another PR that I opened a while back -- here... In case you were actively searching for more stuff to review :-) |
Fixes #15296
When looking at the first argument of a classmethod, the semantic analyzer has special handling if it is
typing.Type[...]. However, as of PEP 585, we can also writebuiltins.type[...]. This PR adds that case, if the signature is from a stub file, if the file importsannotationsfrom__future__, or if the python version is 3.9 or later.The relevant tests I could find use the typing.Type syntax, and I suppose that that should remain the case until 3.9 is the lowest supported python version. For now, I verified that this patch has the desired effect of passing both classes in the repro I gave in issue #15296, but please let me know if more is needed.