Closed mcgibbon closed 1 year ago
Might be worth noting that if I replace the tuples with empty lists, the code runs without an error.
Hi @mcgibbon, thank you for your report!
Short answer is: empty tuple has Tuple[()]
type and TBH I don't know if this is a subtype of Sequence[str]
Tuple has a little bit different typing notation:
Tuple[()]
Tuple[str, ...]
I don't know how to combine it with non-tuple syntax like Sequence[str]
. I will think about it.
I believe Tuples are Sequences, and Tuple[type, ...] is a Sequence[type]. You can't live isinstance when passing the generic type definitions, but:
>>> isinstance((), typing.Sequence)
True
And mypy generally doesn't complain about my code when I pass a Tuple[type, ...] as a Sequence[type]. Here's another test:
$ cat mypy_test.py
from typing import Tuple, Sequence
def foo(a: Sequence[str]):
pass
a: Tuple[()] = ()
foo(a)
$ mypy mypy_test.py
Success: no issues found in 1 source file
Reproducer:
I expect this to run without error. Instead I get:
The issue seems to be due to this behavior inside from_dict:
I would expect _build_value to return an empty tuple.
However, I'm also noticing that
is_instance((), Sequence[str])
givesFalse
, so maybe there's something I'm missing about empty tuples being able to be typed as Sequences in dacite.