Skip to content

Order of declarations matters, but it shouldn't #14519

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

Open
finite-state-machine opened this issue Jan 24, 2023 · 2 comments
Open

Order of declarations matters, but it shouldn't #14519

finite-state-machine opened this issue Jan 24, 2023 · 2 comments
Labels
bug mypy got something wrong

Comments

@finite-state-machine
Copy link

finite-state-machine commented Jan 24, 2023

This replaces #14509.

Bug Report

The order of declarations shouldn't matter to mypy w.r.t.1 type annotations when from __future__ import annotations is in effect, but in the example that follows, they do.

To Reproduce

Analyze the following code with --strict (or run on mypy-play.net):

from __future__ import annotations

import functools
from typing import (
        Any,
        Callable,
        cast,
        Counter,
        Optional,
        TypeVar,
        )


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


def func_wraps(wraps: Tc) -> Callable[[Callable[..., Any]], Tc]:
    '''declare that one function wraps another

    Args:
        wraps: the function being wrapped

    Returns:
        a function 'decorator()', where...
        Args:
            the function which wraps 'wraps'
        Returns:
            the input argument, unchanged

    The type annotations of the return value of the 'decorator()' will
    be those of 'wraps'.
    '''
    def inner(wrapper: Callable[..., Any], /) -> Tc:
        '''decorator

        Args:
            wrapper: the function which wraps 'wraps'
        Returns:
            the same function, unchanged
        '''
        wrapper_ = cast(Tc, wrapper)
        return functools.wraps(wraps)(wrapper_)
    return inner


def function1() -> ChildClass:
    return ChildClass()


class ParentClass:
    def __init__(self, *, param: Optional[int] = None) -> None:
        _ = param

class ChildClass(ParentClass):
    some_counter: Counter[int]

    @func_wraps(ParentClass.__init__)
    def __init__(self, *args: Any, **kwargs: Any):
        super().__init__(*args, **kwargs)
        self.some_counter = Counter()


def function2() -> ChildClass:
    return ChildClass()

Expected Behavior

There should be no errors.

Actual Behavior

The following error is issued w.r.t.1 function1:

bug.py:47: error: Returning Any from function declared to return "ChildClass"  [no-any-return]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.991, master (2023-01-24)
  • Mypy command-line flags: --show-error-codes --strict --warn-unreachable
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.8, 3.11

Footnotes

  1. with respect to 2

@finite-state-machine
Copy link
Author

@erictraut Bumping you just in case this has been overlooked for triage.

@finite-state-machine
Copy link
Author

I suspect this is a special case of #17021, so I recommend diagnosing that issue first.

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