Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
45e5931
Add basic tests, more details will be added when they will not crash
ilevkivskyi Aug 31, 2017
cb4caa5
Correct tests
ilevkivskyi Sep 1, 2017
1cdc980
Implement ForwardRef type, wrap UnboundType, pass SecondPass to third…
ilevkivskyi Sep 11, 2017
260ef02
Add ForwardRefRemover
ilevkivskyi Sep 11, 2017
a58a217
Add elimination patches
ilevkivskyi Sep 11, 2017
950a022
Fix replacement logic; fix newtype error formatting
ilevkivskyi Sep 11, 2017
411b24d
Fix third pass (need to go deeper)
ilevkivskyi Sep 11, 2017
b9b8528
Implement syntethic replacer
ilevkivskyi Sep 11, 2017
48d6de4
Need to go deeper (as usual)
ilevkivskyi Sep 11, 2017
ec45441
Fix postponed fallback join
ilevkivskyi Sep 11, 2017
ac32ed4
Simplify some code and add annotations
ilevkivskyi Sep 11, 2017
3fb3019
Simplify traversal logic; add loads of tests
ilevkivskyi Sep 12, 2017
f9b1320
Take care about one more special case; add few tests and dcostrings
ilevkivskyi Sep 12, 2017
cf014b8
Unify visitors
ilevkivskyi Sep 12, 2017
665236b
Add some more comments and docstrings
ilevkivskyi Sep 12, 2017
9a318aa
Add recursive type warnings
ilevkivskyi Sep 12, 2017
757fbd9
Fix lint
ilevkivskyi Sep 12, 2017
4502ce2
Also clean-up bases; add more tests and allow some previously skipped
ilevkivskyi Sep 13, 2017
3b39d40
One more TypedDict test
ilevkivskyi Sep 13, 2017
c8b28fe
Add another simple self-referrential NamedTuple test
ilevkivskyi Sep 13, 2017
9f92b0f
Fix type_override; add tests for recursive aliases; fix Callable TODO…
Sep 13, 2017
9779103
Merge branch 'master' into fix-synthetic-crashes
ilevkivskyi Sep 14, 2017
b914bdb
Merge remote-tracking branch 'upstream/master' into fix-synthetic-cra…
ilevkivskyi Sep 19, 2017
3568fdb
Skip the whole ForwardRef dance in unchecked functions
ilevkivskyi Sep 19, 2017
54d9331
Simplify test
ilevkivskyi Sep 19, 2017
b9ddacc
Fix self-check
ilevkivskyi Sep 19, 2017
5bfe9ca
Fix cross-file forward references (+test)
ilevkivskyi Sep 19, 2017
a2912e9
More tests
ilevkivskyi Sep 19, 2017
10c65b8
Merge branch 'master' into fix-synthetic-crashes
ilevkivskyi Sep 20, 2017
21dfbfe
Fix situation when recursive namedtuple appears directly in base clas…
ilevkivskyi Sep 20, 2017
f2ddbcd
Merge branch 'fix-synthetic-crashes' of https://github.com/ilevkivsky…
ilevkivskyi Sep 20, 2017
03597ee
Clean-up PR: Remove unnecesary imports, outdated comment, unnecessary…
ilevkivskyi Sep 20, 2017
649ef32
Add tests for generic classes, enums, with statements and for statements
ilevkivskyi Sep 20, 2017
83f8907
Add processing for for and with statements (+more tests)
ilevkivskyi Sep 20, 2017
13c7176
Add support for generic types with forward references
ilevkivskyi Sep 20, 2017
79b10d6
Prohibit forward refs to type vars and subscripted forward refs to al…
ilevkivskyi Sep 21, 2017
321a809
Refactor code to avoid passing semantic analyzer to type analyzer, on…
ilevkivskyi Sep 21, 2017
076c909
Address the rest of the review comments
ilevkivskyi Sep 22, 2017
c1a63ec
Improve two tests
ilevkivskyi Sep 22, 2017
97e6f47
Add one more test as suggested in #3990
ilevkivskyi Sep 23, 2017
8f52654
Address latest review comments
ilevkivskyi Sep 26, 2017
6edd078
Improve tests; Fix one more crash on NewType MRO
ilevkivskyi Sep 27, 2017
514b8bd
Fix formatting in tests
ilevkivskyi Sep 27, 2017
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
Prev Previous commit
Next Next commit
Simplify some code and add annotations
  • Loading branch information
ilevkivskyi committed Sep 11, 2017
commit ac32ed49e888bd7c11c462a6bd99cffd41161c5f
32 changes: 13 additions & 19 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4049,12 +4049,14 @@ class ThirdPass(TraverserVisitor):
straightforward type inference.
"""

def __init__(self, modules: Dict[str, MypyFile], errors: Errors, sem) -> None:
def __init__(self, modules: Dict[str, MypyFile], errors: Errors,
sem: SemanticAnalyzer) -> None:
self.modules = modules
self.errors = errors
self.sem = sem

def visit_file(self, file_node: MypyFile, fnam: str, options: Options, patches) -> None:
def visit_file(self, file_node: MypyFile, fnam: str, options: Options,
patches: List[Callable[[], None]]) -> None:
self.errors.set_file(fnam, file_node.fullname())
self.options = options
self.sem.options = options
Expand Down Expand Up @@ -4211,7 +4213,7 @@ def visit_type_application(self, e: TypeApplication) -> None:

# Helpers

def perform_transform(self, node: Node, transform) -> None:
def perform_transform(self, node: Node, transform: Callable[[Type], Type]) -> None:
if isinstance(node, (FuncDef, CastExpr, AssignmentStmt, TypeAliasExpr, Var)):
node.type = transform(node.type)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What about type_annotation of Argument? Should we process it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I was not able to trigger any errors with this, I will double check.

if isinstance(node, NewTypeExpr):
Expand All @@ -4225,14 +4227,6 @@ def perform_transform(self, node: Node, transform) -> None:
if isinstance(node, TypeApplication):
node.types = [transform(t) for t in node.types]

def remove_forward_refs(self, tp: Type) -> Type:
tp = tp.accept(ForwardRefRemover())
return tp

def replace_synthetic(self, tp: Type) -> Type:
tp = tp.accept(SyntheticReplacer())
return tp

def analyze(self, type: Optional[Type], node: Optional[Node]) -> None:
indicator = {} # type: Dict[str, bool]
if type:
Expand All @@ -4241,12 +4235,12 @@ def analyze(self, type: Optional[Type], node: Optional[Node]) -> None:
type.accept(analyzer)
self.check_for_omitted_generics(type)
if indicator.get('forward') and node is not None:
def patch():
self.perform_transform(node, self.remove_forward_refs)
def patch() -> None:
self.perform_transform(node, lambda tp: tp.accept(ForwardRefRemover()))
self.patches.append(patch)
if indicator.get('synthetic') and node is not None:
def patch():
self.perform_transform(node, self.replace_synthetic)
def patch() -> None:
self.perform_transform(node, lambda tp: tp.accept(SyntheticReplacer()))
self.patches.append(patch)

def check_for_omitted_generics(self, typ: Type) -> None:
Expand Down Expand Up @@ -4738,8 +4732,8 @@ def visit_type_type(self, t: TypeType) -> TypeType:


class ForwardRefRemover(TypeReplacer):
def __init__(self):
self.seen = []
def __init__(self) -> None:
self.seen = [] # type: List[Type]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You could use a Set[int] containing id()s of Type objects for better performance with large types, but the practical impact is slightly minor.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This actually does not work, I get some weird failures, when I change this to a set. I am not sure why, but my guess is that Python reuses ids for deleted objects.


def visit_forwardref_type(self, t: ForwardRef) -> Type:
if not any(s is t for s in self.seen):
Expand All @@ -4749,8 +4743,8 @@ def visit_forwardref_type(self, t: ForwardRef) -> Type:


class SyntheticReplacer(TypeReplacer):
def __init__(self):
self.seen = []
def __init__(self) -> None:
self.seen = [] # type: List[Type]

def visit_tuple_type(self, t: TupleType) -> TupleType:
self.seen.append(t)
Expand Down
7 changes: 5 additions & 2 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Semantic analysis of types"""

from collections import OrderedDict
from typing import Callable, List, Optional, Set, Tuple, Iterator, TypeVar, Iterable
from typing import Callable, List, Optional, Set, Tuple, Iterator, TypeVar, Iterable, Dict
from itertools import chain

from contextlib import contextmanager
Expand Down Expand Up @@ -29,6 +29,9 @@
from mypy.plugin import Plugin, AnalyzerPluginInterface, AnalyzeTypeContext
from mypy import nodes, messages

MYPY = False
if MYPY:
from mypy.semanal import SemanticAnalyzer

T = TypeVar('T')

Expand Down Expand Up @@ -590,7 +593,7 @@ def __init__(self,
fail_func: Callable[[str, Context], None],
options: Options,
is_typeshed_stub: bool,
sem, indicator) -> None:
sem: 'SemanticAnalyzer', indicator: Dict[str, bool]) -> None:
self.fail = fail_func
self.options = options
self.is_typeshed_stub = is_typeshed_stub
Expand Down