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

Support @no_type_check for classes #10162

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,10 @@ def visit_class_def(self, defn: ClassDef) -> None:
self.binder = ConditionalTypeBinder()
with self.binder.top_frame_context():
with self.scope.push_class(defn.info):
self.accept(defn.defs)
if not any(refers_to_fullname(decorator,
'typing.no_type_check')
for decorator in defn.decorators):
self.accept(defn.defs)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Type checking populates some information that other parts of the type checker may rely on. So this needs to be more elaborate.

For example, this shouldn't generate errors:

import typing

@typing.no_type_check
class A:
    a = int()

    def f(self) -> None:
        self.b = int()

a = A()
a.a  # Cannot determine type of "a"
a.b  # Cannot determine type of "b"

One way to deal with this is to give everything in the class the type Any. We'd still no type checking, but it actually wouldn't do much. We use in_checked_function() in mypy.checker to do this. You can add some check for no_type_check decorator there (maybe via a bool attribute flag) so that in_checked_function() returns False inside the class.

self.binder = old_binder
if not (defn.info.typeddict_type or defn.info.tuple_type or defn.info.is_enum):
# If it is not a normal class (not a special form) check class keywords.
Expand All @@ -1733,6 +1736,8 @@ def visit_class_def(self, defn: ClassDef) -> None:
sig = type_object_type(defn.info, self.named_type) # type: Type
# Decorators are applied in reverse order.
for decorator in reversed(defn.decorators):
if refers_to_fullname(decorator, 'typing.no_type_check'):
continue
if (isinstance(decorator, CallExpr)
and isinstance(decorator.analyzed, PromoteExpr)):
# _promote is a special type checking related construct.
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,15 @@ class A:
class B(A): # E: Missing positional argument "x" in call to "__init_subclass__" of "A"
def __init_subclass__(cls) -> None: pass

[case testNoTypeCheckDecoratorOnClass]
import typing

@typing.no_type_check
class A:
def f(self) -> None:
1 + 'x'
[typing fixtures/typing-medium.pyi]

[case testOverrideWithDecorator]
from typing import Callable

Expand Down