Replies: 1 comment 6 replies
-
This suggestion came up during PEP 705's discussion, unfortunately it's ambiguous: class Foo(TypedDict):
x: str
class Bar(TypedDict):
y: ReadOnly[Foo] # what does this mean? Is it a `ReadOnly` version of `Foo` or is the `y` key read-only? A better avenue would be a dedicated type modifier like e.g. The main problem with type modifiers like this however, is that they currently would only be well-defined for Something more generic and flexible would arguably be even better though, which would let you define your own See e.g. how type Partial<T> = {
[P in keyof T]?: T[P];
}; |
Beta Was this translation helpful? Give feedback.
-
It would be quite useful if
TypedDict
instances could be returned as read-only references, similar to how one can return aconst
refence in C++. Here's an example.The
get_user_doc
method is typically used for things like inserting the underlying document into the database, etc, but may also be used to bypass the elaborate property methods above and set anything in the typed dictionary. The only way to get around this today is to use deep copies, so a temporary copy is returned for every call.If a const-reference type could be constructed on the fly by static type checkers, it would create a special class that would have all fields wrapped in
ReadOnly
, such that something like this:, would produce the equivalent of this:
That would allow exposing read-only references to typed dictionaries and would align very well with various ways to control access to object fields, like those
@property
constructs provide.This mechanism would apply only to typed dictionaries, so there's no need to invent the equivalent of
const
methods, like those in C++, and this could be contained fully within the static typing ecosystem.Beta Was this translation helpful? Give feedback.
All reactions