FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

implement @no_type_check for class defs by lunixbochs · Pull Request #6584 · python/mypy · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (4) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 3 additions & 0 deletions mypy/checkmember.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL