You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
% mypy code.py
code.py:4: error: TypedDict() expects a dictionary literal as the second argument
code.py:5: error: TypedDict() expects a dictionary literal as the second argument
(which is quite reasonable, tried that on the off chance it'd work)
@ilevkivskyi I agree that we can just support final names for keys first, at least for NamedTuple, as stated at the end of PEP-591:
Type checkers should treat uses of a final name that was initialized with a literal as if it was replaced by the literal. For example, the following should be allowed:
X: Final = "x"
Y: Final = "y"
N = NamedTuple("N", [(X, int), (Y, int)])
It is sometimes useful to have different types on the same label as well:
X: Final = "x"
Y: Final = "y"
Position = NamedTuple("Position", [(X, int), (Y, int)])
DimensionName = NamedTuple("DimensionName", [(X, str), (Y, str)])
is there any update on this issue when Final is used? This seems last updated about two years ago
I would expect this to be legal:
In [3]: from typing import TypedDict, List, Final
In [6]: X: Final = "namespaces_read"
In [7]: Y: Final = "namespaces_write"
In [4]: Permissions = TypedDict(
...: "Permissions", {X: List[str], Y: List[str]}
...: )
In [8]: Z: Permissions = {X: [], Y: []}
Let me know if you'd like me to remove this and create a separate issue if it's off scope. This falls under the canopy of TypedDict keys reuse, but it looks like this issue came to be about subsets, when I want reuse across keyed structures
Allow all "typed fields" / keyed data structures to reuse their typings.
For me it's not about compliance, neither an open-source library.
It's code repetition.
I would like to define the keys only once.
Reacted by Bi0max, 百地 希留耶, Mo Abrahams, Cesar Canassa, Adelaide あで Fisher, Louis Aumaitre, Ilan Gold, Michał Krassowski, Minki Kim, Katie Byers and 5 more
Activity
ilevkivskyi commentedon Oct 17, 2017
This is currently not possible. We will probably need the
Consttype modifier for this to work, see #1214JukkaL commentedon Nov 16, 2018
We now have a
Finalmodified but that isn't enough, since we never implicitly infer TypedDict types from dict expressions.ilevkivskyi commentedon Dec 20, 2019
Another4 request for this appeared, see #8186, so raising priority to normal.
ilevkivskyi commentedon Dec 20, 2019
(Just to clarify, we can start from only supporting final names for individual keys, which should be pretty easy.)
cwhy commentedon Mar 12, 2020
@ilevkivskyi I agree that we can just support final names for keys first, at least for NamedTuple, as stated at the end of PEP-591:
It is sometimes useful to have different types on the same label as well:
tommyjcarpenter commentedon Mar 7, 2022
is there any update on this issue when Final is used? This seems last updated about two years ago
I would expect this to be legal:
tony commentedon Jul 27, 2022
Let me know if you'd like me to remove this and create a separate issue if it's off scope. This falls under the canopy of
TypedDictkeys reuse, but it looks like this issue came to be about subsets, when I want reuse across keyed structuresAllow all "typed fields" / keyed data structures to reuse their typings.
e.g.
typing.TypedDict,typing.NamedTupleanddataclasses.dataclass.Scenario: Typed exports from dataclass
Assume my case, I want to offer users ability to export a typed tuple and dict from my
Detailsdataclass,dataclasses.astupleanddataclasses.asdictOutput:
Why would a user want to reuse dict/tuple/etc?
strict = TruecomplianceSpreading into functions
*params,**params, e.g. loading dataReuse in
args/kwargsof function declarations, e.g.Problem
We have to redeclare the same field annotations for dict, tuple, and dataclass
Solution
We need a way to reuse the field typings across the different keyed data structures.
nitedani commentedon Sep 30, 2022
For me it's not about compliance, neither an open-source library.
It's code repetition.
I would like to define the keys only once.
Enable mypy strict mode