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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ Ruff is extremely actively developed and used in major open-source projects like

...and [many more](#whos-using-ruff).

Ruff is backed by [Astral](https://astral.sh). Read the [launch post](https://astral.sh/blog/announcing-astral-the-company-behind-ruff),
or the original [project announcement](https://notes.crmarsh.com/python-tooling-could-be-much-much-faster).
Ruff is backed by [Astral](https://astral.sh), the creators of
[uv](https://github.com/astral-sh/uv) and [ty](https://github.com/astral-sh/ty).

Read the [launch post](https://astral.sh/blog/announcing-astral-the-company-behind-ruff), or the
original [project announcement](https://notes.crmarsh.com/python-tooling-could-be-much-much-faster).

## Testimonials

Expand Down
25 changes: 25 additions & 0 deletions crates/ty_python_semantic/resources/mdtest/call/builtins.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ but fall back to `bool` otherwise.
```py
from enum import Enum
from types import FunctionType
from typing import TypeVar

class Answer(Enum):
NO = 0
Expand All @@ -137,6 +138,7 @@ reveal_type(isinstance("", int)) # revealed: bool

class A: ...
class SubclassOfA(A): ...
class OtherSubclassOfA(A): ...
class B: ...

reveal_type(isinstance(A, type)) # revealed: Literal[True]
Expand All @@ -161,6 +163,29 @@ def _(x: A | B, y: list[int]):
else:
reveal_type(x) # revealed: B & ~A
reveal_type(isinstance(x, B)) # revealed: Literal[True]

T = TypeVar("T")
T_bound_A = TypeVar("T_bound_A", bound=A)
T_constrained = TypeVar("T_constrained", SubclassOfA, OtherSubclassOfA)

def _(
x: T,
x_bound_a: T_bound_A,
x_constrained_sub_a: T_constrained,
):
reveal_type(isinstance(x, object)) # revealed: Literal[True]
reveal_type(isinstance(x, A)) # revealed: bool

reveal_type(isinstance(x_bound_a, object)) # revealed: Literal[True]
reveal_type(isinstance(x_bound_a, A)) # revealed: Literal[True]
reveal_type(isinstance(x_bound_a, SubclassOfA)) # revealed: bool
reveal_type(isinstance(x_bound_a, B)) # revealed: bool

reveal_type(isinstance(x_constrained_sub_a, object)) # revealed: Literal[True]
reveal_type(isinstance(x_constrained_sub_a, A)) # revealed: Literal[True]
reveal_type(isinstance(x_constrained_sub_a, SubclassOfA)) # revealed: bool
reveal_type(isinstance(x_constrained_sub_a, OtherSubclassOfA)) # revealed: bool
reveal_type(isinstance(x_constrained_sub_a, B)) # revealed: bool
```

Certain special forms in the typing module are not instances of `type`, so are strictly-speaking
Expand Down
18 changes: 15 additions & 3 deletions crates/ty_python_semantic/src/types/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ use crate::types::{
ClassBase, ClassLiteral, ClassType, DeprecatedInstance, DynamicType, FindLegacyTypeVarsVisitor,
HasRelationToVisitor, IsDisjointVisitor, IsEquivalentVisitor, KnownClass, KnownInstanceType,
NormalizedVisitor, SpecialFormType, SubclassOfInner, SubclassOfType, Truthiness, Type,
TypeContext, TypeMapping, TypeRelation, UnionBuilder, binding_type, definition_expression_type,
infer_definition_types, walk_signature,
TypeContext, TypeMapping, TypeRelation, TypeVarBoundOrConstraints, UnionBuilder, binding_type,
definition_expression_type, infer_definition_types, walk_signature,
};
use crate::{Db, FxOrderSet, ModuleName, resolve_module};

Expand Down Expand Up @@ -1268,6 +1268,19 @@ fn is_instance_truthiness<'db>(

Type::TypeAlias(alias) => is_instance_truthiness(db, alias.value_type(db), class),

Type::TypeVar(bound_typevar) => match bound_typevar.typevar(db).bound_or_constraints(db) {
None => is_instance_truthiness(db, Type::object(), class),
Some(TypeVarBoundOrConstraints::UpperBound(bound)) => {
is_instance_truthiness(db, bound, class)
}
Some(TypeVarBoundOrConstraints::Constraints(constraints)) => always_true_if(
constraints
.elements(db)
.iter()
.all(|c| is_instance_truthiness(db, *c, class).is_always_true()),
),
},

Type::BoundMethod(..)
| Type::KnownBoundMethod(..)
| Type::WrapperDescriptor(..)
Expand All @@ -1281,7 +1294,6 @@ fn is_instance_truthiness<'db>(
| Type::PropertyInstance(..)
| Type::AlwaysTruthy
| Type::AlwaysFalsy
| Type::TypeVar(..)
| Type::BoundSuper(..)
| Type::TypeIs(..)
| Type::Callable(..)
Expand Down
9 changes: 5 additions & 4 deletions crates/ty_python_semantic/src/types/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
//! argument types and return types. For each callable type in the union, the call expression's
//! arguments must match _at least one_ overload.

use std::{collections::HashMap, slice::Iter};
use std::slice::Iter;

use itertools::{EitherOrBoth, Itertools};
use rustc_hash::FxHashMap;
use smallvec::{SmallVec, smallvec_inline};

use super::{DynamicType, Type, TypeVarVariance, definition_expression_type, semantic_index};
Expand Down Expand Up @@ -1324,7 +1325,7 @@ impl<'db> Signature<'db> {
let (self_parameters, other_parameters) = parameters.into_remaining();

// Collect all the keyword-only parameters and the unmatched standard parameters.
let mut self_keywords = HashMap::new();
let mut self_keywords = FxHashMap::default();

// Type of the variadic keyword parameter in `self`.
//
Expand All @@ -1337,7 +1338,7 @@ impl<'db> Signature<'db> {
match self_parameter.kind() {
ParameterKind::KeywordOnly { name, .. }
| ParameterKind::PositionalOrKeyword { name, .. } => {
self_keywords.insert(name.clone(), self_parameter);
self_keywords.insert(name.as_str(), self_parameter);
}
ParameterKind::KeywordVariadic { .. } => {
self_keyword_variadic = Some(self_parameter.annotated_type());
Expand All @@ -1363,7 +1364,7 @@ impl<'db> Signature<'db> {
name: other_name,
default_type: other_default,
} => {
if let Some(self_parameter) = self_keywords.remove(other_name) {
if let Some(self_parameter) = self_keywords.remove(other_name.as_str()) {
match self_parameter.kind() {
ParameterKind::PositionalOrKeyword {
default_type: self_default,
Expand Down
Loading