[issue38834] TypedDict: no way to tell which (if any) keys are optional at runtime

2019-11-24 Thread Ivan Levkivskyi


Change by Ivan Levkivskyi :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38834] TypedDict: no way to tell which (if any) keys are optional at runtime

2019-11-17 Thread Zac Hatfield-Dodds


Change by Zac Hatfield-Dodds :


--
keywords: +patch
pull_requests: +16717
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/17214

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38834] TypedDict: no way to tell which (if any) keys are optional at runtime

2019-11-17 Thread Zac Hatfield-Dodds


New submission from Zac Hatfield-Dodds :

Consider the following cases:

```python
class A(typing.TypedDict):
a: int  # a is required

class B(A, total=False):
b: bool  # a is required, b is optional

class C(B):
c: str  # a is required, b is optional, c is required again
```

PEP-589 is clear about the semantics, and this is obvious enough when reading 
the code.  At runtime the __annotations__ attribute of each class gives us the 
set of allowed keys and the type of each corresponding value, but we have a 
problem:

- C has __total__==True, but b is not actually required.
- B has __total__==False, but a *is* required.
- I can't see any way to get the parent classes of a TypedDict class!

The _TypedDictMeta metaclass updates the attributes, but leaves no record of 
the parent type - at runtime A, B, and C all appear to inherit directly from 
dict.

After discussion on the typing-sig mailing list, I propose to add 
__required_keys__ and __optional_keys__ attributes to TypedDict subclasses, as 
frozensets of strings.

This will be very useful for Hypothesis' `from_type()` strategy, as well as for 
type-based validation frameworks like pydantic or typeguard.

--
components: Library (Lib)
messages: 356836
nosy: Zac Hatfield-Dodds, levkivskyi
priority: normal
severity: normal
status: open
title: TypedDict: no way to tell which (if any) keys are optional at runtime
versions: Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com