Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork131
Add some miscellaneous tests#659
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
+144 −23
Merged
Changes fromall commits
Commits
Show all changes
13 commits Select commitHold shift + click to select a range
b57805e Add miscellaneous tests
brianschubert611cb74 Remove dead branches in _SpecialGenericAlias.__getitem__
brianschubert168aff0 Copy `ParamSpec.__init_subclass__` implementation to Python 3.9 branch
brianschubert69672c2 Add `__[r]or__` tests to `_SpecialForm` instances with meaningful unions
brianschubert8cb7850 Split off `get_type_hints` w/ types.GenericAlias` test
brianschubert50b55af Use public API for testing `_SpecialGenericAlias.__setattr__`
brianschuberted27322 Use mixin for common NoDefault/NoExtraItems tests
brianschubert8032414 name
brianschubert5d7f637 Tidy test_setattr cleanup
brianschubert4a0d76a Use TYPING_3_11_0
brianschuberted5ee71 Add changelog entry
brianschubert29473d1 Merge remote-tracking branch 'upstream/main' into test-coverage
brianschubert2e23850 Remove leftover variable
brianschubertFile 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
7 changes: 7 additions & 0 deletionsCHANGELOG.md
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
143 changes: 129 additions & 14 deletionssrc/test_typing_extensions.py
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 |
|---|---|---|
| @@ -531,6 +531,14 @@ def test_pickle(self): | ||
| pickled = pickle.dumps(self.bottom_type, protocol=proto) | ||
| self.assertIs(self.bottom_type, pickle.loads(pickled)) | ||
| @skipUnless(TYPING_3_10_0, "PEP 604 has yet to be") | ||
| def test_or(self): | ||
| self.assertEqual(self.bottom_type | int, Union[self.bottom_type, int]) | ||
| self.assertEqual(int | self.bottom_type, Union[int, self.bottom_type]) | ||
| self.assertEqual(get_args(self.bottom_type | int), (self.bottom_type, int)) | ||
| self.assertEqual(get_args(int | self.bottom_type), (int, self.bottom_type)) | ||
| class NoReturnTests(BottomTypeTestsMixin, BaseTestCase): | ||
| bottom_type = NoReturn | ||
| @@ -2210,6 +2218,39 @@ def test_or_and_ror(self): | ||
| Union[typing_extensions.Generator, typing.Deque] | ||
| ) | ||
| def test_setattr(self): | ||
| origin = collections.abc.Generator | ||
| alias = typing_extensions.Generator | ||
| original_name = alias._name | ||
| def cleanup(): | ||
| for obj in origin, alias: | ||
| for attr in 'foo', '__dunder__': | ||
| try: | ||
| delattr(obj, attr) | ||
| except Exception: | ||
| pass | ||
| try: | ||
| alias._name = original_name | ||
| except Exception: | ||
| pass | ||
| self.addCleanup(cleanup) | ||
| # Attribute assignment on generic alias sets attribute on origin | ||
| alias.foo = 1 | ||
| self.assertEqual(alias.foo, 1) | ||
| self.assertEqual(origin.foo, 1) | ||
| # Except for dunders... | ||
| alias.__dunder__ = 2 | ||
| self.assertEqual(alias.__dunder__, 2) | ||
| self.assertRaises(AttributeError, lambda: origin.__dunder__) | ||
| # ...and certain known attributes | ||
| alias._name = "NewName" | ||
| self.assertEqual(alias._name, "NewName") | ||
| self.assertRaises(AttributeError, lambda: origin._name) | ||
| class OtherABCTests(BaseTestCase): | ||
| @@ -2379,6 +2420,16 @@ def test_error_message_when_subclassing(self): | ||
| class ProUserId(UserId): | ||
| ... | ||
| def test_module_with_incomplete_sys(self): | ||
| def does_not_exist(*args): | ||
| raise AttributeError | ||
| with ( | ||
| patch("sys._getframemodulename", does_not_exist, create=True), | ||
| patch("sys._getframe", does_not_exist, create=True), | ||
| ): | ||
| X = NewType("X", int) | ||
| self.assertEqual(X.__module__, None) | ||
brianschubert marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| class Coordinate(Protocol): | ||
| x: int | ||
| @@ -5297,6 +5348,17 @@ class A(TypedDict): | ||
| def test_dunder_dict(self): | ||
| self.assertIsInstance(TypedDict.__dict__, dict) | ||
| @skipUnless(TYPING_3_10_0, "PEP 604 has yet to be") | ||
| def test_or(self): | ||
| class TD(TypedDict): | ||
| a: int | ||
| self.assertEqual(TD | int, Union[TD, int]) | ||
| self.assertEqual(int | TD, Union[int, TD]) | ||
| self.assertEqual(get_args(TD | int), (TD, int)) | ||
| self.assertEqual(get_args(int | TD), (int, TD)) | ||
| class AnnotatedTests(BaseTestCase): | ||
| def test_repr(self): | ||
| @@ -5519,6 +5581,19 @@ def barfoo3(x: BA2): ... | ||
| BA2 | ||
| ) | ||
| @skipUnless(TYPING_3_11_0, "TODO: evaluate nested forward refs in Python < 3.11") | ||
| def test_get_type_hints_genericalias(self): | ||
| def foobar(x: list['X']): ... | ||
| X = Annotated[int, (1, 10)] | ||
| self.assertEqual( | ||
| get_type_hints(foobar, globals(), locals()), | ||
| {'x': list[int]} | ||
| ) | ||
| self.assertEqual( | ||
| get_type_hints(foobar, globals(), locals(), include_extras=True), | ||
| {'x': list[Annotated[int, (1, 10)]]} | ||
| ) | ||
| def test_get_type_hints_refs(self): | ||
| Const = Annotated[T, "Const"] | ||
| @@ -5973,6 +6048,11 @@ def run(): | ||
| # The actual test: | ||
| self.assertEqual(result1, result2) | ||
| def test_subclass(self): | ||
| with self.assertRaises(TypeError): | ||
| class MyParamSpec(ParamSpec): | ||
| pass | ||
| class ConcatenateTests(BaseTestCase): | ||
| def test_basics(self): | ||
| @@ -6335,6 +6415,14 @@ def test_pickle(self): | ||
| pickled = pickle.dumps(LiteralString, protocol=proto) | ||
| self.assertIs(LiteralString, pickle.loads(pickled)) | ||
| @skipUnless(TYPING_3_10_0, "PEP 604 has yet to be") | ||
| def test_or(self): | ||
| self.assertEqual(LiteralString | int, Union[LiteralString, int]) | ||
| self.assertEqual(int | LiteralString, Union[int, LiteralString]) | ||
| self.assertEqual(get_args(LiteralString | int), (LiteralString, int)) | ||
| self.assertEqual(get_args(int | LiteralString), (int, LiteralString)) | ||
| class SelfTests(BaseTestCase): | ||
| def test_basics(self): | ||
| @@ -6382,6 +6470,14 @@ def test_pickle(self): | ||
| pickled = pickle.dumps(Self, protocol=proto) | ||
| self.assertIs(Self, pickle.loads(pickled)) | ||
| @skipUnless(TYPING_3_10_0, "PEP 604 has yet to be") | ||
| def test_or(self): | ||
| self.assertEqual(Self | int, Union[Self, int]) | ||
| self.assertEqual(int | Self, Union[int, Self]) | ||
| self.assertEqual(get_args(Self | int), (Self, int)) | ||
| self.assertEqual(get_args(int | Self), (int, Self)) | ||
AlexWaygood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| class UnpackTests(BaseTestCase): | ||
| def test_basic_plain(self): | ||
| @@ -7711,42 +7807,61 @@ class A(Generic[T, P, U]): ... | ||
| self.assertEqual(A[float, [range], int].__args__, (float, (range,), int)) | ||
| classSentinelTestsMixin: | ||
| @skip_if_py313_beta_1 | ||
| def test_pickling(self): | ||
| for proto in range(pickle.HIGHEST_PROTOCOL + 1): | ||
| s = pickle.dumps(self.sentinel_type, proto) | ||
| loaded = pickle.loads(s) | ||
| self.assertIs(self.sentinel_type, loaded) | ||
| @skip_if_py313_beta_1 | ||
| def test_doc(self): | ||
| self.assertIsInstance(self.sentinel_type.__doc__, str) | ||
| def test_constructor(self): | ||
| self.assertIs(self.sentinel_type, type(self.sentinel_type)()) | ||
| with self.assertRaises(TypeError): | ||
| type(self.sentinel_type)(1) | ||
| def test_no_call(self): | ||
| with self.assertRaises(TypeError): | ||
| self.sentinel_type() | ||
| @skip_if_py313_beta_1 | ||
| def test_immutable(self): | ||
| with self.assertRaises(AttributeError): | ||
| self.sentinel_type.foo = 'bar' | ||
| with self.assertRaises(AttributeError): | ||
| self.sentinel_type.foo | ||
| # TypeError is consistent with the behavior of NoneType | ||
| with self.assertRaises(TypeError): | ||
| type(self.sentinel_type).foo = 3 | ||
| with self.assertRaises(AttributeError): | ||
| type(self.sentinel_type).foo | ||
| class NoDefaultTests(SentinelTestsMixin, BaseTestCase): | ||
| sentinel_type = NoDefault | ||
| def test_repr(self): | ||
| if hasattr(typing, 'NoDefault'): | ||
| mod_name = 'typing' | ||
| else: | ||
| mod_name = "typing_extensions" | ||
| self.assertEqual(repr(NoDefault), f"{mod_name}.NoDefault") | ||
| class NoExtraItemsTests(SentinelTestsMixin, BaseTestCase): | ||
| sentinel_type = NoExtraItems | ||
| def test_repr(self): | ||
| if hasattr(typing, 'NoExtraItems'): | ||
| mod_name = 'typing' | ||
| else: | ||
| mod_name = "typing_extensions" | ||
| self.assertEqual(repr(NoExtraItems), f"{mod_name}.NoExtraItems") | ||
| class TypeVarInferVarianceTests(BaseTestCase): | ||
17 changes: 8 additions & 9 deletionssrc/typing_extensions.py
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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.