[ty] Do not assume that fields have a default value#20914
Conversation
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
| field.default_type(db).when_none_or(|default_type| { | ||
| default_type.has_relation_to_impl( | ||
| db, | ||
| right, | ||
| inferable, | ||
| relation, | ||
| relation_visitor, | ||
| disjointness_visitor, | ||
| ) | ||
| }) | ||
| } |
There was a problem hiding this comment.
is this correct? This seems to imply that a Field instance without a default is assignable to literally anything?
Can we make the special case here slightly narrower, like this?
diff --git a/crates/ty_python_semantic/src/types.rs b/crates/ty_python_semantic/src/types.rs
index 8fa72656e4..d538308d80 100644
--- a/crates/ty_python_semantic/src/types.rs
+++ b/crates/ty_python_semantic/src/types.rs
@@ -1634,10 +1634,12 @@ impl<'db> Type<'db> {
// This allows field definitions like `name: str = field(default="")` in dataclasses
// to pass the assignability check of the inferred type to the declared type.
(Type::KnownInstance(KnownInstanceType::Field(field)), right)
- if relation.is_assignability() =>
+ if relation.is_assignability() && field.default_type(db).is_some() =>
{
- field.default_type(db).when_none_or(|default_type| {
- default_type.has_relation_to_impl(
+ field
+ .default_type(db)
+ .expect("Checked in branch arm")
+ .has_relation_to_impl(
db,
right,
inferable,
@@ -1645,7 +1647,6 @@ impl<'db> Type<'db> {
relation_visitor,
disjointness_visitor,
)
- })
}There was a problem hiding this comment.
This seems to imply that a
Fieldinstance without a default is assignable to literally anything?
Yes, I think we want that. Consider something like the following, where the field call is really only there to exclude the id field from the __init__ signature. This field does not have a default value.
id: int = field(init=False)There was a problem hiding this comment.
Ah I see. Fair enough! Do we have tests that will fail if we made the bad refactor I proposed here?
There was a problem hiding this comment.
Yes, all kinds of tests fail if we do this.
df90f82 to
7ea7fb0
Compare
…rable * origin/main: Don't use codspeed or depot runners in CI jobs on forks (#20894) [ty] cache Type::is_redundant_with (#20477) Fix run-away for mutually referential instance attributes (#20645) [ty] Limit shown import paths to at most 5 unless ty runs with `-v` (#20912) [ty] Use field-specifier return type as the default type for the field (#20915) [ty] Do not assume that `field`s have a default value (#20914) [ty] Fix match pattern value narrowing to use equality semantics (#20882) Update setup instructions for Zed 0.208.0+ (#20902) Move TOML indent size config (#20905) [syntax-errors]: implement F702 as semantic syntax error (#20869) [ty] Heterogeneous unpacking support for unions (#20377) [ty] refactor `Place` (#20871) Auto-accept snapshot changes as part of typeshed-sync PRs (#20892) [`airflow`] Add warning to `airflow.datasets.DatasetEvent` usage (`AIR301`) (#20551) [`flake8-pyi`] Fix operator precedence by adding parentheses when needed (`PYI061`) (#20508) [`pyupgrade`] Fix false negative for `TypeVar` with default argument in `non-pep695-generic-class` (`UP046`) (#20660) Update parser snapshots (#20893) Fix syntax error false positives for escapes and quotes in f-strings (#20867)
…nt-sets * dcreager/non-non-inferable: Don't use codspeed or depot runners in CI jobs on forks (#20894) [ty] cache Type::is_redundant_with (#20477) Fix run-away for mutually referential instance attributes (#20645) [ty] Limit shown import paths to at most 5 unless ty runs with `-v` (#20912) [ty] Use field-specifier return type as the default type for the field (#20915) [ty] Do not assume that `field`s have a default value (#20914) [ty] Fix match pattern value narrowing to use equality semantics (#20882) Update setup instructions for Zed 0.208.0+ (#20902) Move TOML indent size config (#20905) [syntax-errors]: implement F702 as semantic syntax error (#20869) [ty] Heterogeneous unpacking support for unions (#20377) [ty] refactor `Place` (#20871) Auto-accept snapshot changes as part of typeshed-sync PRs (#20892) [`airflow`] Add warning to `airflow.datasets.DatasetEvent` usage (`AIR301`) (#20551) [`flake8-pyi`] Fix operator precedence by adding parentheses when needed (`PYI061`) (#20508) [`pyupgrade`] Fix false negative for `TypeVar` with default argument in `non-pep695-generic-class` (`UP046`) (#20660) Update parser snapshots (#20893) Fix syntax error false positives for escapes and quotes in f-strings (#20867)
Summary
fixes astral-sh/ty#1366
Test Plan
Added regression test