Incorrect tests for get_type_hints()
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
A few tests for typing.get_type_hints() test behavior that I feel is incorrect. Fixing it may be more trouble than it's worth, but I thought I'd record these.
- test_get_type_hints_generic
This tests that the a key for the TypedDict is T | None, where T is the TypeVar of that name defined in test_typing.py. However, the relevant TypedDict field is defined in a separate module _typed_dict_helper that has its own T TypeVar. get_type_hints() should pick up the T from the helper module, not the one from test_typing.
This also affects another assertion in the same function.
- test_get_type_hints_classes
This calls get_type_hints() on a class defined like this:
class B(Generic[T]):
class A(Generic[T]):
pass
my_inner_a1: 'B.A'
my_inner_a2: A
my_outer_a: 'A' # unless somebody calls get_type_hints with localns=B.__dict__
It checks that the unquoted A refers to the nested class and the quoted A refers to a different A class in the outer scope. But the fact that the name was quoted shouldn't influence scoping logic; it should resolve to the same name whether or not it's quoted.
I have a change that fixes both of these tests, but I'm not convinced we can afford to change this.
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
- gh-137230
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with test_get_type_hints_generic and test_get_type_hints_classes in Lib/test/test_typing.py, then inspect the referenced definitions in _typed_dict_helper. Run the focused typing tests and review linked PR gh-137230 before proceeding. Done means the assertions and name-resolution expectations reflect the intended get_type_hints() behavior and the relevant tests pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100