Skip to content

don't state that type[Callable] is invalid and don't refer to it as a special form#2207

Open
KotlinIsland wants to merge 1 commit intopython:mainfrom
KotlinIsland:type-callable
Open

don't state that type[Callable] is invalid and don't refer to it as a special form#2207
KotlinIsland wants to merge 1 commit intopython:mainfrom
KotlinIsland:type-callable

Conversation

@KotlinIsland
Copy link
Contributor

@KotlinIsland KotlinIsland commented Mar 6, 2026

see: https://discuss.python.org/t/callable-isnt-a-special-form/106407

Callable is a normal class, not a special form

additionally, there is no reason to disallow type[Callable], i can understand the motivation for disallowing type[Literal[1]] (although i disagree with it), i can't see any reason why type[Callable] isn't allowed when type[Iterable] is

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, Callable is definitely considered a special form. What are you hoping to use type[Callable] for?

@KotlinIsland
Copy link
Contributor Author

Hm, Callable is definitely considered a special form.

why would it be considered a special form? i don't see it as any different to a class like Iterable

What are you hoping to use type[Callable] for?

i have run into problems where type checkers have given incorrect errors because Callable was considered a special form and not a normal class, see the discussion here: https://discuss.python.org/t/callable-isnt-a-special-form/106407 for some examples

@JelleZijlstra
Copy link
Member

why would it be considered a special form?

It is listed specifically in https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions . It's special because it can be parameterized differently from other classes, though perhaps ParamSpec means its specialness can be reduced.

@davidhalter
Copy link
Contributor

I think it's also important to mention that Callable is very much not a normal class in the standard library: https://github.com/python/cpython/blob/b28e5f58ebf8d9badcc893cd189cb1575d4e3614/Lib/typing.py#L2830

and in typeshed: https://github.com/python/typeshed/blob/07ffb67b924d5f532f8b8b72a4902f58faae6aca/stdlib/typing.pyi#L236

@AlexWaygood
Copy link
Member

I think it's also important to mention that Callable is very much not a normal class in the standard library: python/cpython@b28e5f5/Lib/typing.py#L2830

I don't think that this point, specifically, is a very strong argument against this change. While it's true that typing.Callable is not a class at runtime, this is also true for typing.Iterable, typing.Collection, typing.Container, etc... many classes that typeshed pretends are defined in the typing module are actually defined as classes in the collections.abc module at runtime, with only aliases existing in the standard library's typing.py.

I find the fact that Callable has unique semantics in type expressions and cannot be expressed as a Protocol to be much stronger reasons for continuing to describe it as a special form in the spec. That doesn't necessarily mean that it can't be considered a "class-like" special form that is an instance of type.

If we want to consider type[Callable] a valid type annotation, we should state clearly what that means and what the semantics should be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: typing spec For improving the typing spec

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants