| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -709,10 +709,6 @@ def test_or_types_operator(self): | |
| y = int | bool | ||
| with self.assertRaises(TypeError): | ||
| x < y | ||
| # Check that we don't crash if typing.Union does not have a tuple in __args__ | ||
| y = typing.Union[str, int] | ||
| y.__args__ = [str, int] | ||
| self.assertEqual(x, y) | ||
|
|
||
| def test_hash(self): | ||
| self.assertEqual(hash(int | str), hash(str | int)) | ||
| Expand All | @@ -727,17 +723,40 @@ class B(metaclass=UnhashableMeta): ... | |
|
|
||
| self.assertEqual((A | B).__args__, (A, B)) | ||
| union1 = A | B | ||
| with self.assertRaises(TypeError): | ||
| with self.assertRaisesRegex(TypeError, "unhashable type: 'UnhashableMeta'"): | ||
| hash(union1) | ||
|
|
||
| union2 = int | B | ||
| with self.assertRaises(TypeError): | ||
| with self.assertRaisesRegex(TypeError, "unhashable type: 'UnhashableMeta'"): | ||
| hash(union2) | ||
|
|
||
| union3 = A | int | ||
| with self.assertRaises(TypeError): | ||
| with self.assertRaisesRegex(TypeError, "unhashable type: 'UnhashableMeta'"): | ||
| hash(union3) | ||
|
|
||
| def test_unhashable_becomes_hashable(self): | ||
| is_hashable = False | ||
| class UnhashableMeta(type): | ||
| def __hash__(self): | ||
| if is_hashable: | ||
| return 1 | ||
| else: | ||
| raise TypeError("not hashable") | ||
|
|
||
| class A(metaclass=UnhashableMeta): ... | ||
| class B(metaclass=UnhashableMeta): ... | ||
|
|
||
| union = A | B | ||
| self.assertEqual(union.__args__, (A, B)) | ||
|
|
||
| with self.assertRaisesRegex(TypeError, "not hashable"): | ||
| hash(union) | ||
|
|
||
| is_hashable = True | ||
|
|
||
| with self.assertRaisesRegex(TypeError, "union contains 2 unhashable elements"): | ||
| hash(union) | ||
|
|
||
| def test_instancecheck_and_subclasscheck(self): | ||
| for x in (int | str, typing.Union[int, str]): | ||
| with self.subTest(x=x): | ||
| Expand Down Expand Up | @@ -921,7 +940,7 @@ def forward_before(x: ForwardBefore[int]) -> None: ... | |
| self.assertEqual(typing.get_args(typing.get_type_hints(forward_after)['x']), | ||
| (int, Forward)) | ||
| self.assertEqual(typing.get_args(typing.get_type_hints(forward_before)['x']), | ||
| (int, Forward)) | ||
| (Forward, int)) | ||
|
|
||
| def test_or_type_operator_with_Protocol(self): | ||
| class Proto(typing.Protocol): | ||
| Expand Down Expand Up | @@ -1015,9 +1034,14 @@ def __eq__(self, other): | |
| return 1 / 0 | ||
|
|
||
| bt = BadType('bt', (), {}) | ||
| bt2 = BadType('bt2', (), {}) | ||
| # Comparison should fail and errors should propagate out for bad types. | ||
|
Comment thread
Copy link
Copy Markdown
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityWith the new code there are fewer code paths that trigger the equality comparison.
Sorry, something went wrong.
All reactions
|
||
| union1 = int | bt | ||
| union2 = int | bt2 | ||
| with self.assertRaises(ZeroDivisionError): | ||
| union1 == union2 | ||
| with self.assertRaises(ZeroDivisionError): | ||
| list[int] | list[bt] | ||
| bt | bt2 | ||
|
|
||
| union_ga = (list[str] | int, collections.abc.Callable[..., str] | int, | ||
| d | int) | ||
| Expand Down Expand Up | @@ -1060,6 +1084,14 @@ def test_or_type_operator_reference_cycle(self): | |
| self.assertLessEqual(sys.gettotalrefcount() - before, leeway, | ||
| msg='Check for union reference leak.') | ||
|
|
||
| def test_instantiation(self): | ||
| with self.assertRaises(TypeError): | ||
| types.UnionType() | ||
| self.assertIs(int, types.UnionType[int]) | ||
| self.assertIs(int, types.UnionType[int, int]) | ||
| self.assertEqual(int | str, types.UnionType[int, str]) | ||
| self.assertEqual(int | typing.ForwardRef("str"), types.UnionType[int, "str"]) | ||
|
|
||
|
|
||
| class MappingProxyTests(unittest.TestCase): | ||
| mappingproxy = types.MappingProxyType | ||
| Expand Down | ||
| Back | FazBrowse Home | New Git URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality.__args__ is no longer writable.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.