8000 Turn `literal` and `literal_hash` into functions by elazarg · Pull Request #3071 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Turn literal and literal_hash into functions #3071

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

Merged
merged 17 commits into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
tuple -> Tuple[Any, ...]; move isinstance check to literal module; re…
…name to _Hasher
  • Loading branch information
elazarg committed Aug 16, 2017
commit dd4428815e2232d01a79bd26e59249d901fcd07d
7 changes: 3 additions & 4 deletions mypy/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from mypy.join import join_simple
from mypy.sametypes import is_same_type
from mypy.nodes import Expression, Var, RefExpr
from mypy.literals import Key, literal, literal_hash
from mypy.literals import Key, literal, literal_hash, subkeys
from mypy.nodes import IndexExpr, MemberExpr, NameExpr


Expand Down Expand Up @@ -93,9 +93,8 @@ def _add_dependencies(self, key: Key, value: Optional[Key] = None) -> None:
value = key
else:
self.dependencies.setdefault(key, set()).add(value)
for elt in key:
if isinstance(elt, Key):
self._add_dependencies(elt, value)
for elt in subkeys(key):
self._add_dependencies(elt, value)

def push_frame(self) -> Frame:
"""Push a new frame into the binder."""
Expand Down
12 changes: 8 additions & 4 deletions mypy/literals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Union, Any, Tuple
from typing import Optional, Union, Any, Tuple, Iterable

from mypy.nodes import (
Expression, ComparisonExpr, OpExpr, MemberExpr, UnaryExpr, StarExpr, IndexExpr, LITERAL_YES,
Expand Down Expand Up @@ -78,14 +78,18 @@ def literal(e: Expression) -> int:
return LITERAL_NO


Key = tuple
Key = Tuple[Any, ...]


def subkeys(key: Key) -> Iterable[Key]:
return [elt for elt in key if isinstance(elt, tuple)]


def literal_hash(e: Expression) -> Optional[Key]:
return e.accept(_hasher)


class Hasher(ExpressionVisitor[Optional[Key]]):
class _Hasher(ExpressionVisitor[Optional[Key]]):
def visit_int_expr(self, e: IntExpr) -> Key:
return ('Literal', e.value)

Expand Down Expand Up @@ -226,4 +230,4 @@ def visit_temp_node(self, e: TempNode) -> None:
return None


_hasher = Hasher()
_hasher = _Hasher()
0