[issue42592] TypedDict: total=False but still key required

2020-12-07 Thread Brandt Bucher


Brandt Bucher  added the comment:

It looks like this is a duplicate of issue 42059. We should just use their 
existing PR instead (PR 22736).

--
resolution:  -> duplicate
stage: test needed -> resolved
status: open -> closed
superseder:  -> TypedDict(...) as function does not respect "total" when 
setting __required_keys__ and __optional_keys__

___
Python tracker 

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



[issue42592] TypedDict: total=False but still key required

2020-12-07 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

Some notes on needed TypedDict doc fixes.

https://docs.python.org/3/library/typing.html#typing.TypedDict

"class typing.TypedDict(dict)"

The actual signature from inspect.signature is "(typename, fields=None, /, *, 
total=True, **kwargs)".  I presume fields not None and kwargs != {} are 
mutually exclusive.  AFAIK, the kwargs version of the call alternative is not 
in PEP 589.

"The type info for introspection can be accessed via Point2D.__annotations__ 
and Point2D.__total__."

'__total__' is not indexed.  __required_keys__ and __optional_keys__ are 
neither documented (including not in the PEP, which does not get revised) nor 
indexed.

--
nosy: +terry.reedy
stage:  -> test needed
versions: +Python 3.10

___
Python tracker 

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



[issue42592] TypedDict: total=False but still key required

2020-12-07 Thread Brandt Bucher


Change by Brandt Bucher :


--
assignee:  -> brandtbucher

___
Python tracker 

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



[issue42592] TypedDict: total=False but still key required

2020-12-07 Thread Paul Bryan


Paul Bryan  added the comment:

Your patch LGTM, Brandt.

--

___
Python tracker 

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



[issue42592] TypedDict: total=False but still key required

2020-12-07 Thread Brandt Bucher


Brandt Bucher  added the comment:

I can fix this, Paul, unless you want to take it. Probably deserves a 
regression test or two as well.

--

___
Python tracker 

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



[issue42592] TypedDict: total=False but still key required

2020-12-07 Thread Brandt Bucher


Brandt Bucher  added the comment:

It looks like the issue is that _TypedDictMeta only respects "total" as a 
keyword argument to __new__, but the TypedDict function passes it along by 
setting __total__ in the generated namespace instead.

This fixes it:

diff --git a/Lib/typing.py b/Lib/typing.py
index 46c54c4..bb0696b 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -2050,7 +2050,7 @@ class body be required.
 except (AttributeError, ValueError):
 pass
 
-return _TypedDictMeta(typename, (), ns)
+return _TypedDictMeta(typename, (), ns, total=total)
 
 _TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
 TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)

--
nosy: +brandtbucher

___
Python tracker 

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



[issue42592] TypedDict: total=False but still key required

2020-12-07 Thread Paul Bryan


New submission from Paul Bryan :

I believe "a" below should be an optional key, not a required one.

Python 3.9.0 (default, Oct  7 2020, 23:09:01) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import typing
>>> TD = typing.TypedDict("TD", {"a": str}, total=False)
>>> TD.__total__
False
>>> TD.__required_keys__
frozenset({'a'})
>>> TD.__optional_keys__
frozenset()
>>>

--
components: Library (Lib)
messages: 382662
nosy: gvanrossum, pbryan
priority: normal
severity: normal
status: open
title: TypedDict: total=False but still key required
type: behavior
versions: Python 3.9

___
Python tracker 

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