Repository navigation
Basic support for ParamSpec type checking #11594
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
Changes from 1 commit
2007165
da6d041
2506647
56971f0
774a558
cc8fc46
5b8a1c6
bb928ce
f3a2596
ef52db7
c76ce08
8a69923
76ffb14
be6f8e8
c1686b0
a3cb0ad
fd124c5
22afc1d
044ddfd
6d25da4
1047f85
a9b410e
f621fb7
080049e
504267c
2d02b62
dd28b93
01ae7d5
b6b09f1
1af097f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -447,47 +447,54 @@ def deserialize(cls, data: JsonDict) -> 'TypeVarType': | |
| ) | ||
|
|
||
|
|
||
| class ParamSpecFlavor(Enum): | ||
| class ParamSpecFlavor: | ||
| # Simple ParamSpec reference such as "P" | ||
| BARE = 0 | ||
| BARE: Final = 0 | ||
| # P.args | ||
| ARGS = 1 | ||
| ARGS: Final = 1 | ||
| # P.kwargs | ||
| KWARGS = 2 | ||
| KWARGS: Final = 2 | ||
|
|
||
|
|
||
| class ParamSpecType(TypeVarLikeType): | ||
| """Definition of a single ParamSpec variable.""" | ||
|
|
||
| __slots__ = ('flavor',) | ||
|
|
||
| flavor: ParamSpecFlavor | ||
| flavor: int | ||
|
|
||
| def __init__( | ||
| self, name: str, fullname: str, id: Union[TypeVarId, int], flavor: ParamSpecFlavor, | ||
| self, name: str, fullname: str, id: Union[TypeVarId, int], flavor: int, *, | ||
| line: int = -1, column: int = -1 | ||
| ) -> None: | ||
| super().__init__(name, fullname, id, line=line, column=column) | ||
| self.flavor = flavor | ||
|
|
||
| def __repr__(self) -> str: | ||
| return f"<ParamSpecType {self.name}:{self.flavor}>" | ||
|
|
||
| @staticmethod | ||
| def new_unification_variable(old: 'ParamSpecType') -> 'ParamSpecType': | ||
| new_id = TypeVarId.new(meta_level=1) | ||
| return ParamSpecType(old.name, old.fullname, new_id, old.line, old.column) | ||
| return ParamSpecType(old.name, old.fullname, new_id, old.flavor, | ||
| line=old.line, column=old.column) | ||
|
|
||
| def accept(self, visitor: 'TypeVisitor[T]') -> T: | ||
| return visitor.visit_param_spec(self) | ||
|
|
||
| def __hash__(self) -> int: | ||
| return hash((self.id, self.flavor)) | ||
|
|
||
| def __eq__(self, other: object) -> bool: | ||
| if not isinstance(other, ParamSpecType): | ||
| return NotImplemented | ||
| return self.id == other.id and self.flavor == other.flavor | ||
|
Member
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. Why
Collaborator
Author
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. It's not user-configurable and always depends on flavor (actually currently it's always |
||
|
|
||
| def serialize(self) -> JsonDict: | ||
| assert not self.id.is_meta_var() | ||
| return { | ||
| '.class': 'ParamSpecType', | ||
| 'name': self.name, | ||
| 'fullname': self.fullname, | ||
| 'id': self.id.raw_id, | ||
| 'flavor': self.flavor, | ||
| } | ||
|
|
||
| @classmethod | ||
|
|
@@ -497,6 +504,7 @@ def deserialize(cls, data: JsonDict) -> 'ParamSpecType': | |
| data['name'], | ||
| data['fullname'], | ||
| data['id'], | ||
| data['flavor'], | ||
| ) | ||
|
|
||
|
|
||
|
|
||
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
Enum? LikeArgKind: https://github.com/python/mypy/blob/master/mypy/nodes.py#L1636Sorry,
Enums are really my thing 🙂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.
I tend to avoid enums since mypyc only partially supports them, so it's possible to accidentally write code that it's pretty slow.