[ty] Validate signatures of dataclass __post_init__ methods#22730
[ty] Validate signatures of dataclass __post_init__ methods#22730AlexWaygood merged 3 commits intomainfrom
__post_init__ methods#22730Conversation
Typing conformance results improved 🎉The percentage of diagnostics emitted that were expected errors increased from 79.96% to 80.00%. The percentage of expected errors that received a diagnostic increased from 71.23% to 71.42%. Summary
True positives addedDetails
|
|
uff, all that code for a +0.05% improvement in precision and a +0.18% improvement in recall. Still, every little helps, I guess 😆 And this is fairly low-hanging fruit. |
|
a1bc0b3 to
a33e8f8
Compare
crates/ty_python_semantic/resources/mdtest/dataclasses/post_init.md
Outdated
Show resolved
Hide resolved
crates/ty_python_semantic/resources/mdtest/dataclasses/post_init.md
Outdated
Show resolved
Hide resolved
| ); | ||
|
|
||
| let expected_signature = | ||
| CallableType::single(db, Signature::new(parameters, Type::object())); |
There was a problem hiding this comment.
Type::object here is to support the "any return type is allowed" part of your test suite, right? If so, I think Type::any() better models that.
There was a problem hiding this comment.
Type::objecthere is to support the "any return type is allowed" part of your test suite, right?
yup, that's correct!
If so, I think
Type::any()better models that.
hmm... why do you say that? I'd prefer to use fully static types where they express the semantics just as well. In this case, the returned value from __post_init__ is just discarded at runtime -- any object can be returned, so the return type of object feels like it expresses that very well!
There was a problem hiding this comment.
This seemed more about the absence of information to me. object suggests "I want an object", whereas Any suggests "I don't care". It's a subtle difference, definitely not something I'd push too hard on.
a33e8f8 to
3ad7fac
Compare
* main: (76 commits) [ty] Improve the check for `NewType`s with generic bases (#22961) [ty] Ban legacy `TypeVar` bounds or constraints from containing type variables (#22949) Bump the typing conformance suite pin (#22960) [ty] Emit an error if a TypeVarTuple is used to subscript `Generic` or `Protocol` without being unpacked (#22952) [ty] Reduce false positives when subscripting classes generic over `TypeVarTuple`s (#22950) [ty] Detect invalid attempts to subclass `Protocol[]` and `Generic[]` simultaneously (#22948) Fix suppression indentation matching (#22903) Remove hidden `--output-format` warning (#22944) [ty] Validate signatures of dataclass `__post_init__` methods (#22730) [ty] extend special-cased `numbers` diagnostic to `invalid-argument-type` errors (#22938) [ty] Avoid false positive for `not-iterable` with no-positive intersection types (#22089) [ty] Preserve pure negation types in descriptor protocol (#22907) [ty] add special-case diagnostic for `numbers` module (#22931) [ty] Move the location of more `invalid-overload` diagnostics (#22933) [ty] Fix unary and comparison operators for TypeVars with union bounds (#22925) [ty] Rule Selection: ignore/warn/select all rules (unless subsequently overriden) (#22832) [ty] Fix TypedDict construction from existing TypedDict values (#22904) [ty] fix bug in string annotations and clean up diagnostics (#22913) [ty] Improve support for goto-type, goto-declaration, hover, and highlighting of string annotations (#22878) [ty] Rename old typing imports to new on `unresolved-reference`. (#22827) ...
Summary
At runtime, if a dataclass has a
__post_init__method then allInitVarfields are passed to that method as positional arguments (in order of the field definitions). Per tests in the typing conformance suite, a type checker should accordingly verify that a__post_init__method has a compatible signature with theInitVarfields on the class and its dataclass superclasses.Test Plan
Added mdtests (and updated some broken ones I wrote previously, oops)