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

Correctly recognize typing_extensions.NewType #16298

Merged
merged 2 commits into from
Oct 20, 2023
Merged

Correctly recognize typing_extensions.NewType #16298

merged 2 commits into from
Oct 20, 2023

Conversation

gschaffner
Copy link
Contributor

@gschaffner gschaffner commented Oct 20, 2023

fixes #16297.

since the .+_NAMES constants in types.py are each referenced multiple times while other examples like this (i.e. a .+_NAMES tuple/set used only once) are inlined, I've inlined this one.

i added a test for this but for some reason it passes without this patch, even though it fails if it's run manually outside of the test suite. any idea why this is? thank you!

@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@AlexWaygood
Copy link
Member

i added a test for this but for some reason it passes without this patch, even though it fails if it's run manually outside of the test suite. any idea why this is? thank you!

It's almost certainly something to do with the way mypy uses fixtures. If you put the test case in pythoneval.test, it fails on master, but passes with your PR branch. (The test cases in pythoneval.test are run with typeshed's full stubs rather than fixtures, which makes them much slower, but is good for situations like this where the use of fixtures causes spooky behaviour.)

--- a/test-data/unit/pythoneval.test
+++ b/test-data/unit/pythoneval.test
@@ -2118,3 +2118,10 @@ def f(d: Description) -> None:
     reveal_type(d.name_fn)
 [out]
 _testDataclassStrictOptionalAlwaysSet.py:9: note: Revealed type is "def (Union[builtins.int, None]) -> Union[builtins.str, None]"
+
+[case testTypingExtensionsNewType]
+# flags: --python-version 3.7
+from typing_extensions import NewType
+N = NewType("N", int)
+x: N
+[out]

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

Alex is right that this is due to fixtures, but pythoneval tests are relatively expensive, so I'm happy to merge as is

@ilevkivskyi ilevkivskyi marked this pull request as ready for review October 20, 2023 23:28
@ilevkivskyi ilevkivskyi merged commit eecbcb9 into python:master Oct 20, 2023
18 checks passed
@gschaffner
Copy link
Contributor Author

thanks!

should the current test be removed? my understanding is that it cannot detect a regression of this bug, so it may actually be more harmful (providing false confidence) than helpful.

@ilevkivskyi
Copy link
Member

@gschaffner No need to delete the test. Ideally, you may try to fix the test fixtures so that it actually fails on older commits, but then again, as @hauntsaninja this is not very important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

typing_extensions.NewType annotations are not recognized on < 3.10 (regression in 1.4.0)
4 participants