Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
bpo-46571: improve typing.no_type_check to skip foreign objects#31042
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
3 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Test `@no_type_check`, | ||
| # see https://bugs.python.org/issue46571 | ||
| class NoTypeCheck_Outer: | ||
| class Inner: | ||
| x: int | ||
| def NoTypeCheck_function(arg: int) -> int: | ||
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2669,6 +2669,18 @@ def test_errors(self): | ||
| cast('hello', 42) | ||
| # We need this to make sure that `@no_type_check` respects `__module__` attr: | ||
| from test import ann_module8 | ||
| @no_type_check | ||
| class NoTypeCheck_Outer: | ||
| Inner = ann_module8.NoTypeCheck_Outer.Inner | ||
| @no_type_check | ||
| class NoTypeCheck_WithFunction: | ||
| NoTypeCheck_function = ann_module8.NoTypeCheck_function | ||
| class ForwardRefTests(BaseTestCase): | ||
| def test_basics(self): | ||
| @@ -2965,9 +2977,98 @@ def meth(self, x: int): ... | ||
| @no_type_check | ||
| class D(C): | ||
| c = C | ||
| # verify that @no_type_check never affects bases | ||
| self.assertEqual(get_type_hints(C.meth),{'x': int}) | ||
| # and never child classes: | ||
| class Child(D): | ||
| def foo(self, x: int): ... | ||
| self.assertEqual(get_type_hints(Child.foo),{'x': int}) | ||
| def test_no_type_check_nested_types(self): | ||
| # See https://bugs.python.org/issue46571 | ||
| class Other: | ||
| o: int | ||
| class B: # Has the same `__name__`` as `A.B` and different `__qualname__` | ||
| o: int | ||
| @no_type_check | ||
| class A: | ||
| a: int | ||
| class B: | ||
| b: int | ||
| class C: | ||
| c: int | ||
| class D: | ||
| d: int | ||
| Other = Other | ||
| for klass in [A, A.B, A.B.C, A.D]: | ||
| with self.subTest(klass=klass): | ||
| self.assertTrue(klass.__no_type_check__) | ||
| self.assertEqual(get_type_hints(klass),{}) | ||
| for not_modified in [Other, B]: | ||
| with self.subTest(not_modified=not_modified): | ||
| with self.assertRaises(AttributeError): | ||
| not_modified.__no_type_check__ | ||
| self.assertNotEqual(get_type_hints(not_modified),{}) | ||
| def test_no_type_check_class_and_static_methods(self): | ||
| @no_type_check | ||
| class Some: | ||
| @staticmethod | ||
| def st(x: int) -> int: ... | ||
| @classmethod | ||
| def cl(cls, y: int) -> int: ... | ||
| self.assertTrue(Some.st.__no_type_check__) | ||
| self.assertEqual(get_type_hints(Some.st),{}) | ||
| self.assertTrue(Some.cl.__no_type_check__) | ||
| self.assertEqual(get_type_hints(Some.cl),{}) | ||
| def test_no_type_check_other_module(self): | ||
| self.assertTrue(NoTypeCheck_Outer.__no_type_check__) | ||
| with self.assertRaises(AttributeError): | ||
| ann_module8.NoTypeCheck_Outer.__no_type_check__ | ||
| with self.assertRaises(AttributeError): | ||
| ann_module8.NoTypeCheck_Outer.Inner.__no_type_check__ | ||
| self.assertTrue(NoTypeCheck_WithFunction.__no_type_check__) | ||
| with self.assertRaises(AttributeError): | ||
| ann_module8.NoTypeCheck_function.__no_type_check__ | ||
| def test_no_type_check_foreign_functions(self): | ||
| # We should not modify this function: | ||
| def some(*args: int) -> int: | ||
| ... | ||
| @no_type_check | ||
| class A: | ||
| some_alias = some | ||
| some_class = classmethod(some) | ||
| some_static = staticmethod(some) | ||
| with self.assertRaises(AttributeError): | ||
| some.__no_type_check__ | ||
sobolevn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| self.assertEqual(get_type_hints(some),{'args': int, 'return': int}) | ||
| def test_no_type_check_lambda(self): | ||
| @no_type_check | ||
| class A: | ||
| # Corner case: `lambda` is both an assignment and a function: | ||
| bar: Callable[[int], int] = lambda arg: arg | ||
| self.assertTrue(A.bar.__no_type_check__) | ||
| self.assertEqual(get_type_hints(A.bar),{}) | ||
| def test_no_type_check_TypeError(self): | ||
| # This simply should not fail with | ||
| # `TypeError: can't set attributes of built-in/extension type 'dict'` | ||
| no_type_check(dict) | ||
| def test_no_type_check_forward_ref_as_string(self): | ||
| class C: | ||
| foo: typing.ClassVar[int] = 7 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions 4 Misc/NEWS.d/next/Library/2022-02-01-11-21-34.bpo-46571.L40xUJ.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Improve :func:`typing.no_type_check`. | ||
| Now it does not modify external classes and functions. | ||
| We also now correctly mark classmethods as not to be type checked. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.