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
3 changes: 1 addition & 2 deletions crates/ty_python_semantic/resources/mdtest/enums.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,7 @@ class Color(Enum):
BLUE = 3

for color in Color:
# TODO: Should be `Color`
reveal_type(color) # revealed: Unknown
reveal_type(color) # revealed: Color

# TODO: Should be `list[Color]`
reveal_type(list(Color)) # revealed: list[Unknown]
Expand Down
14 changes: 14 additions & 0 deletions crates/ty_python_semantic/src/types/call/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::types::diagnostic::{
NO_MATCHING_OVERLOAD, PARAMETER_ALREADY_ASSIGNED, TOO_MANY_POSITIONAL_ARGUMENTS,
UNKNOWN_ARGUMENT,
};
use crate::types::enums::is_enum_class;
use crate::types::function::{
DataclassTransformerParams, FunctionDecorators, FunctionType, KnownFunction, OverloadLiteral,
};
Expand Down Expand Up @@ -560,6 +561,19 @@ impl<'db> Bindings<'db> {
}
}

// TODO: This branch can be removed once https://github.com/astral-sh/ty/issues/501 is resolved
Type::BoundMethod(bound_method)
if bound_method.function(db).name(db) == "__iter__"
&& is_enum_class(db, bound_method.self_instance(db)) =>
{
if let Some(enum_instance) = bound_method.self_instance(db).to_instance(db)
{
overload.set_return_type(
KnownClass::Iterator.to_specialized_instance(db, [enum_instance]),
);
}
}

Type::FunctionLiteral(function_type) => match function_type.known(db) {
Some(KnownFunction::IsEquivalentTo) => {
if let [Some(ty_a), Some(ty_b)] = overload.parameter_types() {
Expand Down
12 changes: 11 additions & 1 deletion crates/ty_python_semantic/src/types/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2564,6 +2564,7 @@ pub enum KnownClass {
NewType,
SupportsIndex,
Iterable,
Iterator,
// Collections
ChainMap,
Counter,
Expand Down Expand Up @@ -2660,6 +2661,7 @@ impl KnownClass {
| Self::Nonmember
| Self::ABCMeta
| Self::Iterable
| Self::Iterator
// Empty tuples are AlwaysFalse; non-empty tuples are AlwaysTrue
| Self::NamedTuple
// Evaluating `NotImplementedType` in a boolean context was deprecated in Python 3.9
Expand Down Expand Up @@ -2753,6 +2755,7 @@ impl KnownClass {
| Self::OrderedDict
| Self::NewType
| Self::Iterable
| Self::Iterator
| Self::BaseExceptionGroup => false,
}
}
Expand Down Expand Up @@ -2814,6 +2817,7 @@ impl KnownClass {
| KnownClass::NewType
| KnownClass::SupportsIndex
| KnownClass::Iterable
| KnownClass::Iterator
| KnownClass::ChainMap
| KnownClass::Counter
| KnownClass::DefaultDict
Expand Down Expand Up @@ -2842,7 +2846,7 @@ impl KnownClass {
/// 2. It's probably more performant.
const fn is_protocol(self) -> bool {
match self {
Self::SupportsIndex | Self::Iterable => true,
Self::SupportsIndex | Self::Iterable | Self::Iterator => true,

Self::Any
| Self::Bool
Expand Down Expand Up @@ -2968,6 +2972,7 @@ impl KnownClass {
Self::ABCMeta => "ABCMeta",
Self::Super => "super",
Self::Iterable => "Iterable",
Self::Iterator => "Iterator",
// For example, `typing.List` is defined as `List = _Alias()` in typeshed
Self::StdlibAlias => "_Alias",
// This is the name the type of `sys.version_info` has in typeshed,
Expand Down Expand Up @@ -3203,6 +3208,7 @@ impl KnownClass {
| Self::NamedTuple
| Self::StdlibAlias
| Self::Iterable
| Self::Iterator
| Self::SupportsIndex => KnownModule::Typing,
Self::TypeAliasType
| Self::TypeVarTuple
Expand Down Expand Up @@ -3311,6 +3317,7 @@ impl KnownClass {
| Self::Field
| Self::KwOnly
| Self::Iterable
| Self::Iterator
| Self::NamedTupleFallback => false,
}
}
Expand Down Expand Up @@ -3384,6 +3391,7 @@ impl KnownClass {
| Self::Field
| Self::KwOnly
| Self::Iterable
| Self::Iterator
| Self::NamedTupleFallback => false,
}
}
Expand Down Expand Up @@ -3435,6 +3443,7 @@ impl KnownClass {
"TypeAliasType" => Self::TypeAliasType,
"TypeVar" => Self::TypeVar,
"Iterable" => Self::Iterable,
"Iterator" => Self::Iterator,
"ParamSpec" => Self::ParamSpec,
"ParamSpecArgs" => Self::ParamSpecArgs,
"ParamSpecKwargs" => Self::ParamSpecKwargs,
Expand Down Expand Up @@ -3538,6 +3547,7 @@ impl KnownClass {
| Self::TypeVarTuple
| Self::NamedTuple
| Self::Iterable
| Self::Iterator
| Self::NewType => matches!(module, KnownModule::Typing | KnownModule::TypingExtensions),
Self::Deprecated => matches!(module, KnownModule::Warnings | KnownModule::TypingExtensions),

Expand Down
Loading