Skip to content

Decorators over __init__() make classes unconstructable until after the class is declared, even within functions #17021

@finite-state-machine

Description

@finite-state-machine

Bug Report

It should be possible, within a function, to construct an instance of a class which is declared globally later in the module. Applying a decorator factory to a class's __init__() causes constructed instances to have type Any, but only before the class declaration1. (A plain decorator doesn't trigger this issue.)

This probably explains #14519, and may be related to #11293.

To Reproduce

[mypy-play.net]

from __future__ import annotations
from typing import (
        Any,
        Callable,
        TypeVar,
        )
from typing_extensions import (
        assert_type,
        )


Tc = TypeVar('Tc', bound=Callable[..., Any])

def any_decorator_factory() -> Callable[[Tc], Tc]:
    '''this one does nothing
    '''
    def inner(func: Tc) -> Tc:
        return func
    return inner



def function_pre() -> None:
    '''this function is identical to 'function_post()',
       but only this copy generates a mypy error
    '''
    instance = SomeClass()
    assert_type(instance, SomeClass)  # error: type is "Any", not "SomeClass"



class SomeClass:

    @any_decorator_factory()
    def __init__(self):
        pass


def function_post() -> None:
    '''this function is identical to 'function_pre()',
       but only that copy generates a mypy error
    '''
    instance = SomeClass()
    assert_type(instance, SomeClass)  # no error

Expected Behavior

The tool should issue no error for function_pre(); it's just as valid as function_post().

Actual Behavior

Any construction of SomeClass before that type is declared (i.e., before the end of its body) has type Any instead of SomeClass.

Your Environment

  • Mypy version used: master 2024-03-12, 1.9.0
  • Mypy command-line flags: none necessary
  • Mypy configuration options from mypy.ini (and other config files): none necessary
  • Python version used: 3.8, 3.11, 3.12

[Edited for clarity, and to simplify the reproduction case by removing vestigial code.]

Footnotes

  1. some functions within the class body are affected as well; I haven't narrowed down the exact point constructing the class starts to behave normally, but I can do upon request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions