python / python/mypy

With `--disallow-any-decorated`, `Any` detection logic changes w/wo decorator

Open
#15,211 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-disallow-any
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

(A clear and concise description of what the bug is.)

We expect that adding a decorator to a method will never produce a new mypy error unless the decorator itself introduces a new Any annotation, i.e. expect no new error if the decorator itself does not introduce any additional Any.

We also believe that it is difficult to call this behavior a bug. The behavior of the option itself seems self-evident in its own way.

But we would expect a situation where simply applying a harmless decorator would not change the presence or absence of errors.

To Reproduce

Here is a test case.

[case testDisallowAnyDecoratedShouldNeverAffect]
# flags: --disallow-any-decorated

import dataclasses
from typing import Any, Callable

from typing_extensions import TypedDict, TypeVar

class CtxAny(TypedDict):
    place: Any

CallableT = TypeVar("CallableT")

def custom_decorator(func: CallableT) -> CallableT:
    return func

class Something:
    # No error
    def do(self, ctx: CtxAny) -> None:
        pass

    @classmethod
    # This emits an error, unexpected.
    def do_classmethod(cls, ctx: CtxAny) -> None:  # E: Type of decorated function contains type "Any" ("Callable[[Type[Something], CtxAny], None]")
        pass

    @custom_decorator
    # This emits an error, unexpected.
    def do_custom_decorator(self, ctx: CtxAny) -> None:  # E: Type of decorated function contains type "Any" ("Callable[[Something, CtxAny], None]")
        pass
[builtins fixtures/classmethod.pyi]

Expected Behavior

We expect do and others to be the same error-condition.

i.e. one of:

  • No mypy errors emitted.
  • All (containing do method) emits an error.
    • If some option allows us to emit an error on do method, it will be fine.

Actual Behavior

As shown in reproduction code.

Your Environment

  • Mypy version used:current master
  • Mypy command-line flags:--disallow-any-decorated
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.8.16, 3.11.3

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.

Research direction

Start by running the test case testDisallowAnyDecoratedShouldNeverAffect with --disallow-any-decorated and the supplied builtins fixtures/classmethod.pyi fixture. Compare the undecorated, classmethod, and custom-decorator cases, then make the behavior consistent and cover the chosen error condition with regression tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.