I have a couple of {{{TypedDict}}}s for use in type hinting that are supposed
to have some sets of incompatible keys.
For example, if I need to specify that a particular {{{dict}}} can have
{{{bar}}} or {{{baz}}} but not both of them, I can write two {{{TypedDict}}}s:
{{{#!highlight python
from typing import TypedDict
class Foo1(TypedDict):
bar: str
class Foo2(TypedDict):
baz: int
Foo = Foo1 | Foo2
foo_instance_1: Foo = { # Works fine
'bar': 'foobar'
}
foo_instance_2: Foo = { # Also fine
'baz': 42
}
foo_instance_3: Foo = { # Warning: Expected type 'Foo1 | Foo2', got 'dict[str,
str | int]' instead
'bar': 'foobar',
'baz': 42
}
}}}
However, this doesn't scale very well if I were to have multiple sets; for
example, a simple multiplication shows that three sets of 2, 3 and 4 keys each
will result in 24 {{{TypedDict}}}s. A solution would be:
{{{#!highlight python
class Foo(TypedDict):
bar: IncompatibleWith('baz', 'qux')[str]
baz: IncompatibleWith('bar', 'qux')[int]
qux: IncompatibleWith('bar', 'baz')[bool]
}}}
...or, with a decorator somewhat similar to {{{@overload}}}:
{{{#!highlight python
@incompatible('bar', 'baz', 'qux')
# ...
class Foo(TypedDict):
bar: str
baz: int
qux: bool
}}}
I asked this question on StackOverflow<https://stackoverflow.com/q/75981727>
and was informed that such a feature doesn't exist. How about adding it to
Python then?
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/TPILQMOHCWAD7GKDCREONOHZTFY5Z7YW/
Code of Conduct: http://python.org/psf/codeofconduct/