sorted() loses information about namedtuple elements
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
it seems that running sorted() on an union of iterables of named tuple, create some missing information
To Reproduce
from typing import NamedTuple
from typing import reveal_type
class NamedTupleA(NamedTuple):
field_a: str
class NamedTupleB(NamedTuple):
field_b: str
def my_fun(my_list: list[NamedTupleA] | list[NamedTupleB]) -> None:
reveal_type(my_list)
# Revealed type is "Union[builtins.list[tuple[builtins.str, fallback=__main__.NamedTupleA]], builtins.list[tuple[builtins.str, fallback=__main__.NamedTupleB]]]"
for nt in my_list:
if isinstance(nt, NamedTupleA):
print(nt.field_a)
else:
# here nt is recognized of `NamedTupleB` type, I can access field_b
print(nt.field_b)
reveal_type(sorted(my_list))
# Revealed type is "builtins.list[tuple[builtins.str]]"
for sorted_nt in sorted(my_list):
if isinstance(sorted_nt, NamedTupleA):
print(sorted_nt.field_a)
else:
# here I expected nt to be recognized of `NamedTupleB` type
print(sorted_nt.field_b)
if __name__ == '__main__':
my_fun([NamedTupleA('value_1')])
my_fun([NamedTupleB('value_2')])
# this error is expected: I cannot mix NamedTupleA and NamedTupleB
my_fun([NamedTupleA('value_1'), NamedTupleB('value_2')])
basically I have 2 list of NamedTuples.
my_fun must accept either one.
Ideally I could write a User-Defined Type Guards, but let's say that I'm just checking (with isinstance) each element type.
If I loop directly over the list, the check works for both the if and the else branch (there shouldn't be any other case, the union is between two types)
If I loop directly over the sorted list, the type of the element is different, so in the else branch mypy gives me an error
Expected Behavior
I expect no errors on print(sorted_nt.field_b)
Actual Behavior
main.py:32: error: "tuple[str]" has no attribute "field_b" [attr-defined]
Your Environment
linux, python3.12, mypy 1.10.1
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 the linked mypy playground reproduction using Python 3.12 and mypy 1.10.1, then compare the revealed types before and after sorted() in main.py. The fix is complete when the sorted result preserves the NamedTuple alternatives so the isinstance branches accept sorted_nt.field_a and sorted_nt.field_b without an attr-defined error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100