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 @@ -73,7 +73,7 @@ info: rule `static-assert-error` is enabled by default
```

```
error[static-assert-error]: Static assertion error: argument of type `Literal[""]` is statically known to be falsy
error[static-assert-error]: Static assertion error: argument of type `Literal[""]` is always falsy
--> src/mdtest_snippet.py:17:1
|
15 | # evaluates to something falsey
Expand Down
12 changes: 6 additions & 6 deletions crates/ty_python_semantic/resources/mdtest/ty_extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,19 @@ from ty_extensions import static_assert
static_assert(True)
static_assert(False) # error: "Static assertion error: argument evaluates to `False`"

static_assert(None) # error: "Static assertion error: argument of type `None` is statically known to be falsy"
static_assert(None) # error: "Static assertion error: argument of type `None` is always falsy"

static_assert(1)
static_assert(0) # error: "Static assertion error: argument of type `Literal[0]` is statically known to be falsy"
static_assert(0) # error: "Static assertion error: argument of type `Literal[0]` is always falsy"

static_assert((0,))
static_assert(()) # error: "Static assertion error: argument of type `tuple[()]` is statically known to be falsy"
static_assert(()) # error: "Static assertion error: argument of type `tuple[()]` is always falsy"

static_assert("a")
static_assert("") # error: "Static assertion error: argument of type `Literal[""]` is statically known to be falsy"
static_assert("") # error: "Static assertion error: argument of type `Literal[""]` is always falsy"

static_assert(b"a")
static_assert(b"") # error: "Static assertion error: argument of type `Literal[b""]` is statically known to be falsy"
static_assert(b"") # error: "Static assertion error: argument of type `Literal[b""]` is always falsy"
```

### Error messages
Expand Down Expand Up @@ -398,7 +398,7 @@ the expression `str`:
from ty_extensions import TypeOf, is_subtype_of, static_assert

# This is incorrect and therefore fails with ...
# error: "Static assertion error: argument of type `ty_extensions.ConstraintSet` is statically known to be falsy"
# error: "Static assertion error: argument of type `ConstraintSet[Literal[False]]` is always falsy"
static_assert(is_subtype_of(str, type[str]))

# Correct, returns True:
Expand Down
14 changes: 12 additions & 2 deletions crates/ty_python_semantic/src/types/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::semantic_index::scope::{FileScopeId, ScopeKind};
use crate::semantic_index::semantic_index;
use crate::types::callable::CallableTypeKind;
use crate::types::class::{ClassLiteral, ClassType, GenericAlias};
use crate::types::constraints::ConstraintSetBuilder;
use crate::types::function::{FunctionType, OverloadLiteral};
use crate::types::generics::{GenericContext, Specialization};
use crate::types::signatures::{
Expand Down Expand Up @@ -2965,8 +2966,17 @@ impl<'db> FmtDetailed<'db> for DisplayKnownInstanceRepr<'db> {
}
Ok(())
}
KnownInstanceType::ConstraintSet(_) => {
f.with_type(ty).write_str("ty_extensions.ConstraintSet")
KnownInstanceType::ConstraintSet(interned_set) => {
f.with_type(ty).write_str("ConstraintSet")?;
let constraints = ConstraintSetBuilder::new();
let set = constraints.load(self.db, interned_set.constraints(self.db));
if set.is_always_satisfied(self.db) {
f.write_str("[Literal[True]]")
} else if set.is_never_satisfied(self.db) {
f.write_str("[Literal[False]]")
} else {
f.write_str("[bool]")
}
}
KnownInstanceType::GenericContext(generic_context) => {
f.with_type(ty).write_str("ty_extensions.GenericContext")?;
Expand Down
2 changes: 1 addition & 1 deletion crates/ty_python_semantic/src/types/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1987,7 +1987,7 @@ impl KnownFunction {
} else if truthiness.is_always_false() {
builder.into_diagnostic(format_args!(
"Static assertion error: argument of type `{parameter_ty}` \
is statically known to be falsy",
is always falsy",
parameter_ty = parameter_ty.display(db)
))
} else {
Expand Down
Loading