-
-
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
Add function signature hooks for dataclasses functions: replace, asdict, astuple #5152
Comments
How would one go about to fix the type signature for function replace<X extends Record<string, any>>(base: X, changes: Partial<X>): X {
return {...base, ...changes}
} It's not a 100% correspondence, I'm using TS' |
This hole is allowing errors to leak through in some of my production code. Lacking a 'typeof' operator as well, it's difficult to ensure the types are correct. |
Hello and thank you for creating mypy!! If I may, I'd like to make a case for this issue.
I can only assume that this isn't a trivial issue to fix. It seems I haven't made a strong case for this issue after all--I've only managed to express my frustration 😢. It's just that the next time someone asks me if it's possible to do typed functional programming in Python, I'd love to say "Yes!" instead of "Almost!" |
With If the def replace(obj: _T, **changes: _T.__PARTIAL_TYPED_DICT__) -> _T: ... Otherwise we could try something like #14526 where we determine the signature in a function sig hook. Curiously while in |
FWIW I remember I thought #8583 was an OK solution. But we also need to support other methods: |
Validate `dataclassses.replace` actual arguments to match the fields: - Unlike `__init__`, the arguments are always named. - All arguments are optional except for `InitVar`s without a default value. The tricks: - We're looking up type of the first positional argument ("obj") through private API. See #10216, #14845. - We're preparing the signature of "replace" (for that specific dataclass) during the dataclass transformation and storing it in a "private" class attribute `__mypy-replace` (obviously not part of PEP-557 but contains a hyphen so should not conflict with any future valid identifier). Stashing the signature into the symbol table allows it to be passed across phases and cached across invocations. The stashed signature lacks the first argument, which we prepend at function signature hook time, since it depends on the type that `replace` is called on. Based on #14526 but actually simpler. Partially addresses #5152. # Remaining tasks - [x] handle generic dataclasses - [x] avoid data class transforms - [x] fine-grained mode tests --------- Co-authored-by: Alex Waygood <[email protected]>
|
Currently, the plugin only supports dataclass creation (i.e. generation of dunder methods with correct types, including
__init__
). However, some functions in thedataclass
modules have quite broad types in typeshed, for example:so that arbitrary arguments can be give for
changes
, etc. The plugin should be able to give more precise type for these functions. In particular,asdict
could return aTypedDict
.The text was updated successfully, but these errors were encountered: