Skip to content

implement @no_type_check for class defs #6584

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

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
3 changes: 3 additions & 0 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ def analyze_instance_member_access(name: str,
if override_info:
info = override_info

if info.is_no_type_check:
return AnyType(TypeOfAny.special_form)

if (state.find_occurrences and
info.name() == state.find_occurrences[0] and
name == state.find_occurrences[1]):
Expand Down
1 change: 1 addition & 0 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2219,6 +2219,7 @@ class is generic then it will be a type constructor of higher kind.
metaclass_type = None # type: Optional[mypy.types.Instance]

names = None # type: SymbolTable # Names defined directly in this type
is_no_type_check = False # Was the class annotated with @no_type_check?
is_abstract = False # Does the class have any abstract attributes?
is_protocol = False # Is this a protocol class?
runtime_protocol = False # Does this protocol support isinstance checks?
Expand Down
11 changes: 11 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,9 @@ def prepare_method_signature(self, func: FuncDef, info: TypeInfo) -> None:
"""Check basic signature validity and tweak annotation of self/cls argument."""
# Only non-static methods are special.
functype = func.type
if info.is_no_type_check:
func.type = AnyType(TypeOfAny.special_form)
return
if not func.is_static:
if not func.arguments:
self.fail('Method must have at least one argument', func)
Expand Down Expand Up @@ -807,6 +810,14 @@ def visit_class_def(self, defn: ClassDef) -> None:
self.analyze_class(defn)

def analyze_class(self, defn: ClassDef) -> None:
if defn.decorators:
for decorator in reversed(defn.decorators):
if isinstance(decorator, NameExpr) and \
decorator.name in ('no_type_check', 'typing.no_type_check'):
defn.info.is_no_type_check = True
defn.info.fallback_to_any = True
return

is_protocol = self.detect_protocol_base(defn)
self.update_metaclass(defn)
self.clean_up_bases_and_infer_type_variables(defn)
Expand Down
2 changes: 2 additions & 0 deletions mypy/semanal_pass3.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ def visit_overloaded_func_def(self, fdef: OverloadedFuncDef) -> None:
super().visit_overloaded_func_def(fdef)

def visit_class_def(self, tdef: ClassDef) -> None:
if tdef.info.is_no_type_check:
return
# NamedTuple base classes are validated in check_namedtuple_classdef; we don't have to
# check them again here.
self.scope.enter_class(tdef.info)
Expand Down