-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Add LiteralType and stub methods to type visitors #5934
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,19 @@ | |
|
||
JsonDict = Dict[str, Any] | ||
|
||
# The set of all valid expressions that can currently be contained | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
# inside of a Literal[...]. | ||
# | ||
# Literals can contain enum-values: we special-case those and | ||
# store the value as a string. | ||
# | ||
# Note: this type also happens to correspond to types that can be | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is very useful, thanks! |
||
# directly converted into JSON. The serialize/deserialize methods | ||
# of 'LiteralType' rely on this, as well 'server.astdiff.SnapshotTypeVisitor' | ||
# and 'types.TypeStrVisitor'. If we end up adding any non-JSON-serializable | ||
# types to this list, we should make sure to edit those methods to match. | ||
LiteralInnerExpr = Union[int, str, bool, None] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we need to be able to distinguish between A name like |
||
|
||
# If we only import type_visitor in the middle of the file, mypy | ||
# breaks, and if we do it at the top, it breaks at runtime because of | ||
# import cycle issues, so we do it at the top while typechecking and | ||
|
@@ -1240,6 +1253,43 @@ def zipall(self, right: 'TypedDictType') \ | |
yield (item_name, None, right_item_type) | ||
|
||
|
||
class LiteralType(Type): | ||
__slots__ = ('value', 'fallback') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add docstring or at least a |
||
|
||
def __init__(self, value: LiteralInnerExpr, fallback: Instance, | ||
line: int = -1, column: int = -1) -> None: | ||
super().__init__(line, column) | ||
self.value = value | ||
self.fallback = fallback | ||
|
||
def accept(self, visitor: 'TypeVisitor[T]') -> T: | ||
return visitor.visit_literal_type(self) | ||
|
||
def __hash__(self) -> int: | ||
return hash((self.value, self.fallback)) | ||
|
||
def __eq__(self, other: object) -> bool: | ||
if isinstance(other, LiteralType): | ||
return self.fallback == other.fallback and self.value == other.value | ||
else: | ||
return NotImplemented | ||
|
||
def serialize(self) -> Union[JsonDict, str]: | ||
return { | ||
'.class': 'LiteralType', | ||
'value': self.value, | ||
'fallback': self.fallback.serialize(), | ||
} | ||
|
||
@classmethod | ||
def deserialize(cls, data: JsonDict) -> 'LiteralType': | ||
assert data['.class'] == 'LiteralType' | ||
return LiteralType( | ||
value=data['value'], | ||
fallback=Instance.deserialize(data['fallback']), | ||
) | ||
|
||
|
||
class StarType(Type): | ||
"""The star type *type_parameter. | ||
|
||
|
@@ -1693,6 +1743,9 @@ def item_str(name: str, typ: str) -> str: | |
suffix = ', fallback={}'.format(t.fallback.accept(self)) | ||
return 'TypedDict({}{}{})'.format(prefix, s, suffix) | ||
|
||
def visit_literal_type(self, t: LiteralType) -> str: | ||
return 'Literal[{}]'.format(t.value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use |
||
|
||
def visit_star_type(self, t: StarType) -> str: | ||
s = t.type.accept(self) | ||
return '*{}'.format(s) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe return
t
unmodified instead.