[ty] Fix visibility of field specifiers when models are nested inside methods#23069
Merged
[ty] Fix visibility of field specifiers when models are nested inside methods#23069
Conversation
Typing conformance resultsNo changes detected ✅ |
5669ab7 to
a96f014
Compare
a96f014 to
8a2308b
Compare
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
not-subscriptable |
96 | 1 | 0 |
unsupported-operator |
89 | 0 | 0 |
possibly-missing-attribute |
3 | 4 | 36 |
no-matching-overload |
0 | 21 | 0 |
invalid-argument-type |
0 | 2 | 7 |
invalid-parameter-default |
0 | 0 | 7 |
invalid-return-type |
0 | 1 | 3 |
unresolved-attribute |
2 | 0 | 2 |
invalid-await |
0 | 2 | 0 |
not-iterable |
0 | 0 | 2 |
invalid-assignment |
0 | 0 | 1 |
missing-argument |
1 | 0 | 0 |
unused-type-ignore-comment |
1 | 0 | 0 |
| Total | 192 | 31 | 58 |
sharkdp
commented
Feb 4, 2026
Comment on lines
+473
to
+501
| fn field_specifiers<'db>( | ||
| db: &'db dyn Db, | ||
| index: &'db SemanticIndex<'db>, | ||
| scope: ScopeId<'db>, | ||
| ) -> Option<SmallVec<[Type<'db>; NUM_FIELD_SPECIFIERS_INLINE]>> { | ||
| let enclosing_scope = index.scope(scope.file_scope_id(db)); | ||
| let class_node = enclosing_scope.node().as_class()?; | ||
| let class_definition = index.expect_single_definition(class_node); | ||
| let class_literal = infer_definition_types(db, class_definition) | ||
| .declaration_type(class_definition) | ||
| .inner_type() | ||
| .as_class_literal()? | ||
| .as_static()?; | ||
|
|
||
| class_literal | ||
| .dataclass_params(db) | ||
| .map(|params| SmallVec::from(params.field_specifiers(db))) | ||
| .or_else(|| { | ||
| Some(SmallVec::from( | ||
| CodeGeneratorKind::from_class(db, class_literal.into(), None)? | ||
| .dataclass_transformer_params()? | ||
| .field_specifiers(db), | ||
| )) | ||
| }) | ||
| } | ||
|
|
||
| if let Some(specifiers) = field_specifiers(self.db(), self.index, self.scope()) { | ||
| self.dataclass_field_specifiers = specifiers; | ||
| } |
Contributor
Author
There was a problem hiding this comment.
This has only been moved out into a function.
carljm
approved these changes
Feb 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The right hand side of annotated assignments inside methods of a class are treated as standalone expressions. Therefore, if someone defines a dataclass-transformer model inside a method like this …
… we infer the right hand side of
x: int = field(kw_only=True)as a standalone expression. Previously though, we did not set up theTypeInferenceBuilder::dataclass_field_specifiersfield in those cases. This meant thatfieldwas not recognized as a field-specifier function.Ecosystem impact
These are unavoidable, I believe. They would require an
attrs plugin. We have similar errors elsewhere already. These new diagnostics only appear because we previously thought that those fields had default values (since we didn't recognize the right hand side as a field-specifier call).Test Plan
Regression test.