Skip to content

Improve typing._GenericAlias.__dir__ coverage #103395

Closed
@sobolevn

Description

@sobolevn

Right now the only test we have for dir() on _GenericAlias is

    def test_genericalias_dir(self):
        class Foo(Generic[T]):
            def bar(self):
                pass
            baz = 3
        # 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]))

And here's how it is defined:

cpython/Lib/typing.py

Lines 1323 to 1325 in c330b4a

def __dir__(self):
return list(set(super().__dir__()
+ [attr for attr in dir(self.__origin__) if not _is_dunder(attr)]))

We clearly need more tests:

  1. That dunder methods are not included
  2. What _GenericAlias API we expose (at least parts that we consider user-visible, like __args__ and __parameters__ and __origin__)
  3. _GenericAlias has subclasses, they are also not tested

I will send a PR for this :)

Linked PRs

Metadata

Metadata

Assignees

Labels

testsTests in the Lib/test dirtopic-typingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions