How about adding an additional proerty type to TypeDict
When handling json schema data, there is the concept of additional properties. For a given map there are - required key value pairs with known string keys and known value types - optional key value pairs with known string keys and known value types (pairs may be omitted) - optional key value pairs with unknown string keys and known value types (pairs may be omitted) Here is an example of what an implementation could look like it uses additional_prop_type to define the additional properties type ``` class SomeMapRequired(typing.AddPropsTypedDict): a: int class SomeMapOptional(typing.AddPropsTypedDict, total=False, additional_prop_type=float): b: str class SomeMap(SomeMapRequired, SomeMapOptional): pass inst: SomeMap = {'a': 1} inst: SomeMap = {'a': 1, 'b': ''} inst: SomeMap = {'a': 1, 'someAddProp': 1.23} inst: SomeMap = {'a': 1, 'someAddProp': None} # fails, additional_prop_type wrong ``` Having this would help immensely with storing type hints for json schema data.
This was previously here: https://mail.python.org/archives/list/typing-sig@python.org/thread/66RITIHDQ... I think there might be enough interest to make this happen, though I don't really like the proposed syntax.
I'm also interested in having this added, but I'm wondering why the proposed syntax is so complicated. In my mind, this can be implemented in TypedDict without additional classes or keyword arguments. By using the TypedDict(name, {...}) call form and allowing types as keys in the provided dict. pyright already allows combining the class syntax with the call syntax, so it can be used even in the class form. In fact, this would be perfect use case for anonymous implicit TypedDicts that use the dict[{...}] syntax. Example code: ```python # Currently supported in pyright, but not in mypy class OldTypedDict(TypedDict('SomeNameForATypedDict',{'a': str})): b: int d1: OldTypedDict = {'a': 'a', 'b': 1} # pass d2: OldTypedDict = {'a': 'a', 'b': 'bad'} # fail # Extending with "type" keys class TypedDictFromExplicitTD(TypedDict('SomeNameForATypedDict',{'a': str, str: float})): # notice the "str: float" to indicate that all dict keys of type str without explicit key name should have values of type float b: int d3: TypedDictFromExplicitTD = {'a': 'a', 'b': 1, 'some_unexpected_key': 1.2} # pass d4: TypedDictFromExplicitTD = {'a': 'a', 'b': 1, 'some_unexpected_key': 'bad'} # fail # Using the implict TypedDict syntax class TypedDictFromImplicitTD(dict[{'a': str, str: float}]): b: int d5: TypedDictFromImplicitTD = {'a': 'a', 'b': 1, 'some_unexpected_key': 1.2} # pass d6: TypedDictFromImplicitTD = {'a': 'a', 'b': 1, 'some_unexpected_key': 'bad'} # fail # Using Any: Any to signal that any key and value type is allowed TypedDictEverythingGoes = TypedDict('TypedDictEverythingGoes', {'a': str, Any: Any}) # only 'a' matters, you don't care about the rest of the dict d7: TypedDictEverythingGoes = {'a': 'a', 'some_unexpected_key': 1.2} # pass d8: TypedDictEverythingGoes = {'a': 'a', 'some_unexpected_key': 'bad'} # pass d9: TypedDictEverythingGoes = {'a': 1.0, 'some_unexpected_key': 1.2} # fail ``` ---- ORIGINAL MESSAGE ---- Date: 2023-06-13 14:04:04 UTC+0200 From:tmke8@posteo.net To:typing-sig@python.org Subject: [Typing-sig] Re: How about adding an additional proerty type to TypeDict
This was previously here:https://mail.python.org/archives/list/typing-sig@python.org/thread/66RITIHDQ...
I think there might be enough interest to make this happen, though I don't really like the proposed syntax. _______________________________________________ Typing-sig mailing list --typing-sig@python.org To unsubscribe send an email totyping-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address:dev@reggx.eu
Would anyone like to write a pep adding this functionality? For context Typescript has the ability to constrain the types of additional properties in this way. I would be happy to collaborate with someone.
Warning: before you spend too much time on writing a PEP, make sure at least one core dev supports this idea. See PEP 1 for the rules about sponsorship of PEPs authored by non-core-devs. On Mon, Jun 19, 2023 at 5:41 PM Justin Black <justin.a.black@gmail.com> wrote:
Would anyone like to write a pep adding this functionality? For context Typescript has the ability to constrain the types of additional properties in this way. I would be happy to collaborate with someone. _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: guido@python.org
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c...>
It sounds like you can express the presence of additional entries with known value types via an intersection: SomeMap & dict[str, T]. Intersections aren't currently part of the Python type system, but if they above works for your use case, I think it'd strictly better to focus on specifying intersections instead of adding more features to TypedDict. Sergei On Mon, 12 Jun 2023, 23:47 Justin Black, <justin.a.black@gmail.com> wrote:
When handling json schema data, there is the concept of additional properties. For a given map there are - required key value pairs with known string keys and known value types - optional key value pairs with known string keys and known value types (pairs may be omitted) - optional key value pairs with unknown string keys and known value types (pairs may be omitted)
Here is an example of what an implementation could look like it uses additional_prop_type to define the additional properties type ``` class SomeMapRequired(typing.AddPropsTypedDict): a: int
class SomeMapOptional(typing.AddPropsTypedDict, total=False, additional_prop_type=float): b: str
class SomeMap(SomeMapRequired, SomeMapOptional): pass
inst: SomeMap = {'a': 1} inst: SomeMap = {'a': 1, 'b': ''} inst: SomeMap = {'a': 1, 'someAddProp': 1.23} inst: SomeMap = {'a': 1, 'someAddProp': None} # fails, additional_prop_type wrong ``` Having this would help immensely with storing type hints for json schema data. _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: sergei.a.lebedev@gmail.com
participants (5)
-
Guido van Rossum
-
Justin Black
-
ReggX
-
Sergei Lebedev
-
Thomas Kehrenberg