8000 gh-103395: Improve `typing._GenericAlias.__dir__` coverage by sobolevn · Pull Request #103396 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-103395: Improve typing._GenericA 8000 lias.__dir__ coverage #103396

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
merged 2 commits into from
Apr 10, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,15 @@ def test_accepts_single_type(self):
(*tuple[int],)
Unpack[Tuple[int]]

def test_dir(self):
dir_items = set(dir(Unpack[Tuple[int]]))
for required_item in [
'__args__', '__parameters__', '__origin__',
'copy_with',
]:
with self.subTest(required_item=required_item):
self.assertIn(required_item, dir_items)

def test_rejects_multiple_types(self):
with self.assertRaises(TypeError):
Unpack[Tuple[int], Tuple[str]]
Expand Down Expand Up @@ -1699,6 +1708,15 @@ def test_repr(self):
u = Optional[str]
self.assertEqual(repr(u), 'typing.Optional[str]')

def test_dir(self):
dir_items = set(dir(Union[str, int]))
for required_item in [
'__args__', '__parameters__', '__origin__',
'copy_with',
]:
with self.subTest(required_item=required_item):
self.assertIn(required_item, dir_items)

def test_cannot_subclass(self):
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Union'):
Expand Down Expand Up @@ -1839,6 +1857,17 @@ def test_eq_hash(self):
self.assertNotEqual(C, Callable[..., int])
self.assertNotEqual(C, Callable)

def test_dir(self):
Callable = self.Callable
dir_items = set(dir(Callable[..., int]))
for required_item in [
'__args__', '__parameters__', '__origin__',
# TODO: add to `collections.abc.Callable`:
# 'copy_with',
]:
with self.subTest(required_item=required_item):
self.assertIn(required_item, dir_items)

def test_cannot_instantiate(self):
Callable = self.Callable
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -2151,6 +2180,15 @@ def test_repr(self):
self.assertEqual(repr(Literal[None]), "typing.Literal[None]")
self.assertEqual(repr(Literal[1, 2, 3, 3]), "typing.Literal[1, 2, 3]")

def test_dir(self):
dir_items = set(dir(Literal[1, 2, 3]))
for required_item in [
'__args__', '__parameters__', '__origin__',
'copy_with',
]:
with self.subTest(required_item=required_item):
self.assertIn(required_item, dir_items)

def test_cannot_init(self):
with self.assertRaises(TypeError):
Literal()
Expand Down Expand Up @@ -7315,6 +7353,16 @@ def test_repr(self):
"typing.Annotated[typing.List[int], 4, 5]"
)

def test_dir(self):
dir_items = set(dir(Annotated[int, 4]))
for required_item in [
'__args__', '__parameters__', '__origin__',
'__metadata__',
Copy link
Member
@AlexWaygood AlexWaygood Apr 9, 2023

Choose a reason for hiding this comment

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

Note that the __metadata__ field is not currently documented, but really should be:

(No change requested here.)

'copy_with',
]:
with self.subTest(required_item=required_item):
self.assertIn(required_item, dir_items)

def test_flatten(self):
A = Annotated[Annotated[int, 4], 5]
self.assertEqual(A, Annotated[int, 4, 5])
Expand Down Expand Up @@ -8033,6 +8081,16 @@ class MyClass: ...
c = Concatenate[MyClass, P]
self.assertNotEqual(c, Concatenate)

def test_dir(self):
P = ParamSpec('P')
dir_items = set(dir(Concatenate[int, P]))
for required_item in [
'__args__', '__parameters__', '__origin__',
'copy_with',
]:
with self.subTest(required_item=required_item):
self.assertIn(required_item, dir_items)

def test_valid_uses(self):
P = ParamSpec('P')
T = TypeVar('T')
Expand Down Expand Up @@ -8310,10 +8368,19 @@ class Foo(Generic[T]):
def bar(self):
pass
baz = 3
__magic__ = 4

# The class attributes of the original class should be visible even
# in dir() of the GenericAlias. See bpo-45755.
self.assertIn('bar', dir(Foo[int]))
self.assertIn('baz', dir(Foo[int]))
dir_items = set(dir(Foo[int]))
for required_item in [
'bar', 'baz',
'__args__', '__parameters__', '__origin__',
'copy_with',
]:
with self.subTest(required_item=required_item):
self.assertIn(required_item, dir_items)
self.assertNotIn('__magic__', dir_items)


class RevealTypeTests(BaseTestCase):
Expand Down
0