Checking call signatures of `Type[C]` objects
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
No file, test, or entry point is named. Start by reviewing mypy's handling of Type[C] calls, init compatibility, and classmethod cls types, then compare the proposed Type signature forms with PEP 484 and typing constraints. Done requires an agreed design for type-object call signatures before implementation can be scoped.
Written by the indexing model from the issue text.
Description
Currently mypy doesn't complain about this code:
from typing import Type
class A:
def __init__(self, x: int) -> None: pass
def f(t: Type[object]) -> None:
t() # ok, since `object()` is valid
f(A) # no error reported!
This is caused by __init__ overrides not needing to be compatible with base classes.
It's not clear how to type check such code. Here are some ideas, some of which have been discussed before:
- We could reject calling
Type[C]and requireType[[ArgType, ...], C]for type objects that can be called. The argument types would specify a signature for__init__. This would require a PEP 484 (and atypingchange). This has the problem of being backward incompatible. Additionally, it's not clear how to specify signatures with keyword arguments or optional arguments. - Similar to above, but by default
Type[C]would take the__init__signature fromC, similar to how it behaves now. Unlike now,Type[D]would only be a subtype ofType[C]if the__init__signature is compatible.Type[None, C]would be a type object that is not callable.Type[[X, ...], C]would also be supported and would support specifying a specific signature instead of just taking it fromC. Type[..., C]would be a callable type object which accepts arbitrary arguments.Type[[T], C[T]]would perhaps also be valid for generic classes (Tis a type variable).
Class methods also have this issue. We could support specifying the type of cls explicitly. For example:
class A:
...
@classmethod
def f(cls: Type[[int], A]) -> A:
return cls(1) # ok
(I known that this is an old issue, but I couldn't find a previous open issue.)
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 54
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/mypy
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
bug topic-configuration topic-error-reporting
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
zostera/django-bootstrap4#894 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
use-agent-os/agent-os#3276 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
NousResearch/hermes-agent#117848 ·