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
14 changes: 13 additions & 1 deletion crates/ty_python_semantic/resources/mdtest/protocols.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,21 @@ def f(
# fmt: on
```

Nonetheless, `Protocol` can still be used as the second argument to `issubclass()` at runtime:
Nonetheless, `Protocol` is an instance of `type` at runtime, and therefore can still be used as the
second argument to `issubclass()` at runtime:

```py
import abc
import typing
from ty_extensions import TypeOf

reveal_type(type(Protocol)) # revealed: <class '_ProtocolMeta'>
# revealed: tuple[<class '_ProtocolMeta'>, <class 'ABCMeta'>, <class 'type'>, <class 'object'>]
reveal_type(type(Protocol).__mro__)
static_assert(is_subtype_of(TypeOf[Protocol], type))
static_assert(is_subtype_of(TypeOf[Protocol], abc.ABCMeta))
static_assert(is_subtype_of(TypeOf[Protocol], typing._ProtocolMeta))

# Could also be `Literal[True]`, but `bool` is fine:
reveal_type(issubclass(MyProtocol, Protocol)) # revealed: bool
```
Expand Down
13 changes: 12 additions & 1 deletion crates/ty_python_semantic/src/types/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3694,6 +3694,7 @@ pub enum KnownClass {
ParamSpec,
ParamSpecArgs,
ParamSpecKwargs,
ProtocolMeta,
TypeVarTuple,
TypeAliasType,
NoDefaultType,
Expand Down Expand Up @@ -3821,6 +3822,7 @@ impl KnownClass {
| Self::NamedTupleFallback
| Self::NamedTupleLike
| Self::ConstraintSet
| Self::ProtocolMeta
| Self::TypedDictFallback => Some(Truthiness::Ambiguous),

Self::Tuple => None,
Expand Down Expand Up @@ -3903,6 +3905,7 @@ impl KnownClass {
| KnownClass::ConstraintSet
| KnownClass::TypedDictFallback
| KnownClass::BuiltinFunctionType
| KnownClass::ProtocolMeta
| KnownClass::Template => false,
}
}
Expand Down Expand Up @@ -3982,6 +3985,7 @@ impl KnownClass {
| KnownClass::ConstraintSet
| KnownClass::TypedDictFallback
| KnownClass::BuiltinFunctionType
| KnownClass::ProtocolMeta
| KnownClass::Template => false,
}
}
Expand Down Expand Up @@ -4060,6 +4064,7 @@ impl KnownClass {
| KnownClass::NamedTupleFallback
| KnownClass::ConstraintSet
| KnownClass::BuiltinFunctionType
| KnownClass::ProtocolMeta
| KnownClass::Template => false,
}
}
Expand Down Expand Up @@ -4151,6 +4156,7 @@ impl KnownClass {
| Self::ConstraintSet
| Self::TypedDictFallback
| Self::BuiltinFunctionType
| Self::ProtocolMeta
| Self::Template => false,
}
}
Expand Down Expand Up @@ -4250,6 +4256,7 @@ impl KnownClass {
Self::ConstraintSet => "ConstraintSet",
Self::TypedDictFallback => "TypedDictFallback",
Self::Template => "Template",
Self::ProtocolMeta => "_ProtocolMeta",
}
}

Expand Down Expand Up @@ -4477,6 +4484,7 @@ impl KnownClass {
| Self::StdlibAlias
| Self::Iterable
| Self::Iterator
| Self::ProtocolMeta
| Self::SupportsIndex => KnownModule::Typing,
Self::TypeAliasType
| Self::TypeVarTuple
Expand Down Expand Up @@ -4596,6 +4604,7 @@ impl KnownClass {
| Self::ConstraintSet
| Self::TypedDictFallback
| Self::BuiltinFunctionType
| Self::ProtocolMeta
| Self::Template => Some(false),

Self::Tuple => None,
Expand Down Expand Up @@ -4680,6 +4689,7 @@ impl KnownClass {
| Self::ConstraintSet
| Self::TypedDictFallback
| Self::BuiltinFunctionType
| Self::ProtocolMeta
| Self::Template => false,
}
}
Expand Down Expand Up @@ -4773,6 +4783,7 @@ impl KnownClass {
"ConstraintSet" => Self::ConstraintSet,
"TypedDictFallback" => Self::TypedDictFallback,
"Template" => Self::Template,
"_ProtocolMeta" => Self::ProtocolMeta,
_ => return None,
};

Expand Down Expand Up @@ -4855,9 +4866,9 @@ impl KnownClass {
| Self::TypeVarTuple
| Self::Iterable
| Self::Iterator
| Self::ProtocolMeta
| Self::NewType => matches!(module, KnownModule::Typing | KnownModule::TypingExtensions),
Self::Deprecated => matches!(module, KnownModule::Warnings | KnownModule::TypingExtensions),

}
}

Expand Down
6 changes: 5 additions & 1 deletion crates/ty_python_semantic/src/types/special_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,13 @@ impl SpecialFormType {
| Self::Bottom
| Self::Intersection
| Self::CallableTypeOf
| Self::Protocol // actually `_ProtocolMeta` at runtime but this is what typeshed says
| Self::ReadOnly => KnownClass::SpecialForm,

// Typeshed says it's an instance of `_SpecialForm`,
// but then we wouldn't recognise things like `issubclass(`X, Protocol)`
// as being valid.
Self::Protocol => KnownClass::ProtocolMeta,

Self::Generic | Self::Any => KnownClass::Type,

Self::List
Expand Down
Loading