-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for default types in TypedDict #6131
Comments
Thanks for the proposal! Unfortunately, I don't think we will have time to work on this in short term, but we may raise priority if this request will appear again. Also a random note: same functionality is available for protocols using |
We'd need a way to represent this using the class-based syntax as well. |
In class-based syntax this could plausibly be supported as:
|
I have a usecase this would be extremely helpful for as well, in the context of a physical modeling app. We have EXTREMELY large dictionaries of physical properties passed to models. They're mostly floating point measurements except for a small minority of values that are strings or nested dicts. Using a default type of float and only specifying the exceptions could improve concision by hundreds of lines. |
Please make this happen |
I join the requests - it will be super convenient if I could write: class MyClass(TypedDict):
a: int = 1
b: int = 10
v: Optional[int] = None |
If anyone has a specific proposal to solve this please post to typing-sig. |
@oren0e Why not just use the class Employee(NamedTuple):
name: str
id: int = 3 |
@AIGeneratedUsername, from typing import NamedTuple, TypedDict, Optional
class EmployeeTuple(NamedTuple):
name: str
age: Optional[int] = 18
class EmployeeDict(TypedDict):
name: str
age: int
def show_employee(age: int, name: str) -> None:
print(f"{name} is {age} years old.")
empdict = EmployeeDict(name='Alice', age=18)
emptup = EmployeeTuple(name='Bob')
show_employee(**empdict) # "Alice is 18 years old." Works as expected.
show_employee(*emptup) # "18 is Bob years old." Doesn't work.
show_employee(**emptup) # TypeError: show_employee() argument after ** must be a mapping, not EmployeeTuple |
Is this going to be implemented? Feels a bit weird missing default values here |
If you want this you should ask on the typing tracker https://github.com/python/typing/discussions |
Thanks, maybe it would make sense to close this issue here then |
You might be able to get away with using the from typing import Annotated, Optional
class MyClass(TypedDict):
a: Annotated[int, 1]
b: Annotated[int, 10]
v: Annotated[Optional[int], None] NOTE: Variables can't be used in the class definition unless its a type alias, so you can't simply write DefaultArg = Annotated and use that because you have to specify the indeces to `DefaultIntArg = Annotated[int, 1] # defeats the purpose
Apologies, the discussion is at python/typing#1165 |
I started migrating all TypedDicts to dataclasses due to this issue, as dataclasses support this feature. |
Good for you. A coding style using dataclasses is much more readable than one using dictionaries as records. |
Hi @tommyjcarpenter, how would you do that? How do you add a default type for undefined attributes in a dataclass? Thanks. |
This problem arose from trying to type the
environ
argument for a WSGI application. There are specific keys of theenviron
dict that have non-str
types, and then all the other keys areHTTP_
variables (or server-defined variables, which can be ignored for this purpose) with typestr
(orText
, depending on the version of Python).I would like to get stronger typing on
environ
thanDict[str, Any]
, but there isn't a way to do that withTypedDict
currently because the set of possible keys is unbounded (the client can send any HTTP header).Ideally, I'd like to be able to do something like:
and have this mean that any key is allowed in the dictionary and all unknown keys get a value type of
str
.The text was updated successfully, but these errors were encountered: