Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
finite-state-machine opened this issue Mar 13, 2024 · 0 comments
Labels
bug mypy got something wrong

Comments

@finite-state-machine
Copy link

finite-state-machine commented Mar 13, 2024

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.

@finite-state-machine finite-state-machine added the bug mypy got something wrong label Mar 13, 2024
@finite-state-machine finite-state-machine changed the title Decorators over __init__() make classes unconstructable until after the class is declared, even wtihin functions Decorators over __init__() make classes unconstructable until after the class is declared, even within functions Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant