KEMBAR78
Fix compatibility checks for conditional function definitions using decorators by brianschubert · Pull Request #18020 · python/mypy · GitHub
Skip to content

Conversation

brianschubert
Copy link
Member

Fixes #17211, resolves this # TODO:

def dec(f) -> Callable[[int], None]: pass
x = int()
if x:
def f(x: str) -> None: pass
else:
# TODO: Complain about incompatible redefinition
@dec
def f(): pass

Before

from typing import Callable

def dec(f: object) -> Callable[[int], None]: raise NotImplementedError

if int():
    def f(x: str) -> None: pass
else:
    @dec
    def f() -> None: pass  # uh oh! passes without error

After

from typing import Callable

def dec(f: object) -> Callable[[int], None]: raise NotImplementedError

if int():
    def f(x: str) -> None: pass
else:
    @dec
    def f() -> None: pass  # E: All conditional function variants must have identical signatures \
                           # N: Original: \
                           # N:     def f(x: str) -> None \
                           # N: Redefinition: \
                           # N:     def f(int, /) -> None

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the changes to the overload tests don't look quite right. This is meant to support this feature: #12606

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

operator (https://github.com/canonical/operator)
+ ops/framework.py:601: error: Incompatible redefinition (redefinition with type "Callable[[Framework], FrameworkEvents]", original type "FrameworkEvents")  [misc]
+ ops/charm.py:1330: error: Incompatible redefinition (redefinition with type "Callable[[CharmBase], CharmEvents]", original type "CharmEvents")  [misc]

pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/_numba/executor.py:90: error: Incompatible redefinition (redefinition with type "Callable[[ndarray[Any, Any], ndarray[Any, Any], ndarray[Any, Any], int, VarArg(Any)], Any]", original type "Callable[[ndarray[Any, Any], ndarray[Any, Any], int, int, VarArg(Any)], Any]")  [misc]

@hauntsaninja
Copy link
Collaborator

Primer hits look correct!

@brianschubert
Copy link
Member Author

@hauntsaninja Thanks the quick review! I may be reading this wrong, but I think the feature from #12606 is still working correctly. It's only the "broken" / unsupported condition versions in testOverloadIfNestedFailure that now emit an extra error. The "correct" versions in testOverloadIfNestedOk pass with no new errors.

I'll take a look to see if I can avoid the new errors, though I'm guessing that may be hard for similar reasons to why those overloads currently emit E: Single overload definition, multiple required and Name "xyz" already defined errors.

I concur on the primer hits :-)

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, I missed that! In that case, this looks great. Thanks for the fix!

@hauntsaninja hauntsaninja merged commit 60d1b37 into python:master Oct 23, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Conditional decorated functions are not type checked

2 participants