Checking call signatures of `Type[C]` objects

Open
#1,831 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
python
Domain
compilers

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

needs discussion priority-2-low topic-calls topic-inheritance

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 require Type[[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 a typing change). 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 from C, similar to how it behaves now. Unlike now, Type[D] would only be a subtype of Type[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 from C.
  • Type[..., C] would be a callable type object which accepts arbitrary arguments.
  • Type[[T], C[T]] would perhaps also be valid for generic classes (T is 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/mypy

All issues in python/mypy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.