On 17 May 2023, at 14:16, Sync In <insyncwith...@gmail.com> wrote:
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?
You may get a reply here but all the python type discussions take place on https://discuss.python.org these days. Suggest you ask there.
Barry _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/TPILQMOHCWAD7GKDCREONOHZTFY5Z7YW/ Code of Conduct: http://python.org/psf/codeofconduct/
|
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/KXX2FASV4CXN2PSMC2RPX2NL5YH25WZC/
Code of Conduct: http://python.org/psf/codeofconduct/