Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -960,5 +960,21 @@ def f(x: Not) -> None:
reveal_type(x) # revealed: Unknown
```

## Attribute access on pure negation types

When an attribute has a pure negation type (an intersection with only negative contributions, like
`~AlwaysFalsy`), accessing it should preserve the negation information rather than falling back to
`object`.

```py
from ty_extensions import Not, AlwaysFalsy

class C:
x: Not[AlwaysFalsy]

def f(c: C):
reveal_type(c.x) # revealed: ~AlwaysFalsy
```

[complement laws]: https://en.wikipedia.org/wiki/Complement_(set_theory)
[de morgan's laws]: https://en.wikipedia.org/wiki/De_Morgan%27s_laws
30 changes: 17 additions & 13 deletions crates/ty_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2792,28 +2792,32 @@ impl<'db> Type<'db> {
},
),

PlaceAndQualifiers {
attribute @ PlaceAndQualifiers {
place:
Place::Defined(DefinedPlace {
ty: Type::Intersection(intersection),
origin,
definedness: boundness,
definedness,
widening,
}),
qualifiers,
} => (
intersection
.map_with_boundness(db, |elem| {
Place::Defined(DefinedPlace {
ty: elem
.try_call_dunder_get(db, instance, owner)
.map_or(*elem, |(ty, _)| ty),
origin,
definedness: boundness,
widening,
if intersection.positive(db).is_empty() {
attribute
} else {
intersection
.map_with_boundness(db, |elem| {
Place::Defined(DefinedPlace {
ty: elem
.try_call_dunder_get(db, instance, owner)
.map_or(*elem, |(ty, _)| ty),
origin,
definedness,
widening,
})
})
})
.with_qualifiers(qualifiers),
.with_qualifiers(qualifiers)
},
// TODO: Discover data descriptors in intersections.
AttributeKind::NormalOrNonDataDescriptor,
),
Expand Down