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.
Uh oh!
There was an error while loading. Please reload this page.
-
It would be quite useful if
TypedDictinstances could be returned as read-only references, similar to how one can return aconstrefence in C++. Here's an example.The
get_user_docmethod 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
@propertyconstructs provide.This mechanism would apply only to typed dictionaries, so there's no need to invent the equivalent of
constmethods, like those in C++, and this could be contained fully within the static typing ecosystem.Beta Was this translation helpful? Give feedback.
All reactions