Skip to content

Commit

Permalink
Upgrade pyre (Instagram#1032)
Browse files Browse the repository at this point in the history
* Upgrade pyre

* regen fixtures
  • Loading branch information
zsol authored and manmartgarc committed Oct 3, 2023
1 parent fd59f44 commit 14df046
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 89 deletions.
1 change: 0 additions & 1 deletion libcst/_nodes/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def _visit_and_replace_children(self, visitor: CSTVisitorT) -> "Module":
has_trailing_newline=self.has_trailing_newline,
)

# pyre-fixme[14]: `visit` overrides method defined in `CSTNode` inconsistently.
def visit(self: _ModuleSelfT, visitor: CSTVisitorT) -> _ModuleSelfT:
"""
Returns the result of running a visitor over this module.
Expand Down
6 changes: 1 addition & 5 deletions libcst/codemod/commands/convert_type_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def _parse_type_comment(
if type_comment is None:
return None
try:
# pyre-ignore[16]: the ast module stubs do not have full details
return ast.parse(type_comment, "<type_comment>", "eval").body
except SyntaxError:
return None
Expand All @@ -69,10 +68,7 @@ def _parse_func_type_comment(
) -> Optional["ast.FunctionType"]:
if func_type_comment is None:
return None
return cast(
ast.FunctionType,
ast.parse(func_type_comment, "<func_type_comment>", "func_type"),
)
return ast.parse(func_type_comment, "<func_type_comment>", "func_type")


@functools.lru_cache()
Expand Down
61 changes: 12 additions & 49 deletions libcst/matchers/_matcher_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class AbstractBaseMatcherNodeMeta(ABCMeta):
matcher.
"""

# pyre-fixme[14]: `__or__` overrides method defined in `type` inconsistently.
# pyre-fixme[15]: `__or__` overrides method defined in `type` inconsistently.
def __or__(self, node: Type["BaseMatcherNode"]) -> "TypeOf[Type[BaseMatcherNode]]":
return TypeOf(self, node)
Expand All @@ -84,25 +83,16 @@ class BaseMatcherNode:
several concrete matchers as options.
"""

# pyre-fixme[14]: `__or__` overrides method defined in `type` inconsistently.
# pyre-fixme[15]: `__or__` overrides method defined in `type` inconsistently.
def __or__(
self: _BaseMatcherNodeSelfT, other: _OtherNodeT
) -> "OneOf[Union[_BaseMatcherNodeSelfT, _OtherNodeT]]":
# Without a cast, pyre thinks that the below OneOf is type OneOf[object]
# even though it has the types passed into it.
return cast(
OneOf[Union[_BaseMatcherNodeSelfT, _OtherNodeT]], OneOf(self, other)
)
return OneOf(self, other)

def __and__(
self: _BaseMatcherNodeSelfT, other: _OtherNodeT
) -> "AllOf[Union[_BaseMatcherNodeSelfT, _OtherNodeT]]":
# Without a cast, pyre thinks that the below AllOf is type AllOf[object]
# even though it has the types passed into it.
return cast(
AllOf[Union[_BaseMatcherNodeSelfT, _OtherNodeT]], AllOf(self, other)
)
return AllOf(self, other)

def __invert__(self: _BaseMatcherNodeSelfT) -> "_BaseMatcherNodeSelfT":
return cast(_BaseMatcherNodeSelfT, _InverseOf(self))
Expand Down Expand Up @@ -180,7 +170,6 @@ def __call__(self, *args: object, **kwargs: object) -> BaseMatcherNode:
self._call_items = (args, kwargs)
return self

# pyre-fixme[14]: `__or__` overrides method defined in `type` inconsistently.
# pyre-fixme[15]: `__or__` overrides method defined in `type` inconsistently.
def __or__(
self, other: _OtherNodeMatcherTypeT
Expand Down Expand Up @@ -240,19 +229,16 @@ def options(self) -> Sequence[_MatcherT]:
"""
return self._options

# pyre-fixme[14]: `__or__` overrides method defined in `type` inconsistently.
# pyre-fixme[15]: `__or__` overrides method defined in `type` inconsistently.
def __or__(self, other: _OtherNodeT) -> "OneOf[Union[_MatcherT, _OtherNodeT]]":
# Without a cast, pyre thinks that the below OneOf is type OneOf[object]
# even though it has the types passed into it.
return cast(OneOf[Union[_MatcherT, _OtherNodeT]], OneOf(self, other))
return OneOf(self, other)

def __and__(self, other: _OtherNodeT) -> NoReturn:
raise Exception("Cannot use AllOf and OneOf in combination!")

def __invert__(self) -> "AllOf[_MatcherT]":
# Invert using De Morgan's Law so we don't have to complicate types.
return cast(AllOf[_MatcherT], AllOf(*[DoesNotMatch(m) for m in self._options]))
return AllOf(*[DoesNotMatch(m) for m in self._options])

def __repr__(self) -> str:
return f"OneOf({', '.join([repr(o) for o in self._options])})"
Expand Down Expand Up @@ -318,19 +304,16 @@ def options(self) -> Sequence[_MatcherT]:
"""
return self._options

# pyre-fixme[14]: `__or__` overrides method defined in `type` inconsistently.
# pyre-fixme[15]: `__or__` overrides method defined in `type` inconsistently.
def __or__(self, other: _OtherNodeT) -> NoReturn:
raise Exception("Cannot use AllOf and OneOf in combination!")

def __and__(self, other: _OtherNodeT) -> "AllOf[Union[_MatcherT, _OtherNodeT]]":
# Without a cast, pyre thinks that the below AllOf is type AllOf[object]
# even though it has the types passed into it.
return cast(AllOf[Union[_MatcherT, _OtherNodeT]], AllOf(self, other))
return AllOf(self, other)

def __invert__(self) -> "OneOf[_MatcherT]":
# Invert using De Morgan's Law so we don't have to complicate types.
return cast(OneOf[_MatcherT], OneOf(*[DoesNotMatch(m) for m in self._options]))
return OneOf(*[DoesNotMatch(m) for m in self._options])

def __repr__(self) -> str:
return f"AllOf({', '.join([repr(o) for o in self._options])})"
Expand Down Expand Up @@ -367,7 +350,6 @@ def matcher(self) -> _MatcherT:
"""
return self._matcher

# pyre-fixme[14]: `__or__` overrides method defined in `type` inconsistently.
# pyre-fixme[15]: `__or__` overrides method defined in `type` inconsistently.
def __or__(self, other: _OtherNodeT) -> "OneOf[Union[_MatcherT, _OtherNodeT]]":
# Without a cast, pyre thinks that the below OneOf is type OneOf[object]
Expand Down Expand Up @@ -438,7 +420,6 @@ def name(self) -> str:
"""
return self._name

# pyre-fixme[14]: `__or__` overrides method defined in `type` inconsistently.
# pyre-fixme[15]: `__or__` overrides method defined in `type` inconsistently.
def __or__(self, other: _OtherNodeT) -> "OneOf[Union[_MatcherT, _OtherNodeT]]":
# Without a cast, pyre thinks that the below OneOf is type OneOf[object]
Expand Down Expand Up @@ -512,25 +493,16 @@ def func(self) -> Callable[[_MatchIfTrueT], bool]:
"""
return self._func

# pyre-fixme[14]: `__or__` overrides method defined in `type` inconsistently.
# pyre-fixme[15]: `__or__` overrides method defined in `type` inconsistently.
def __or__(
self, other: _OtherNodeT
) -> "OneOf[Union[MatchIfTrue[_MatchIfTrueT], _OtherNodeT]]":
# Without a cast, pyre thinks that the below OneOf is type OneOf[object]
# even though it has the types passed into it.
return cast(
OneOf[Union[MatchIfTrue[_MatchIfTrueT], _OtherNodeT]], OneOf(self, other)
)
return OneOf(self, other)

def __and__(
self, other: _OtherNodeT
) -> "AllOf[Union[MatchIfTrue[_MatchIfTrueT], _OtherNodeT]]":
# Without a cast, pyre thinks that the below AllOf is type AllOf[object]
# even though it has the types passed into it.
return cast(
AllOf[Union[MatchIfTrue[_MatchIfTrueT], _OtherNodeT]], AllOf(self, other)
)
return AllOf(self, other)

def __invert__(self) -> "MatchIfTrue[_MatchIfTrueT]":
# Construct a wrapped version of MatchIfTrue for typing simplicity.
Expand Down Expand Up @@ -560,7 +532,6 @@ def MatchRegex(regex: Union[str, Pattern[str]]) -> MatchIfTrue[str]:

def _match_func(value: object) -> bool:
if isinstance(value, str):
# pyre-ignore Pyre doesn't think a 'Pattern' can be passed to fullmatch.
return bool(re.fullmatch(regex, value))
else:
return False
Expand Down Expand Up @@ -642,15 +613,12 @@ def value(self) -> object:
"""
return self._value

# pyre-fixme[14]: `__or__` overrides method defined in `type` inconsistently.
# pyre-fixme[15]: `__or__` overrides method defined in `type` inconsistently.
def __or__(self, other: _OtherNodeT) -> "OneOf[Union[MatchMetadata, _OtherNodeT]]":
# Without the cast, pyre doesn't know this is valid
return cast(OneOf[Union[MatchMetadata, _OtherNodeT]], OneOf(self, other))
return OneOf(self, other)

def __and__(self, other: _OtherNodeT) -> "AllOf[Union[MatchMetadata, _OtherNodeT]]":
# Without the cast, pyre doesn't know this is valid
return cast(AllOf[Union[MatchMetadata, _OtherNodeT]], AllOf(self, other))
return AllOf(self, other)

def __invert__(self) -> "MatchMetadata":
# We intentionally lie here, for the same reason given in the documentation
Expand Down Expand Up @@ -728,19 +696,16 @@ def func(self) -> Callable[[object], bool]:
"""
return self._func

# pyre-fixme[14]: `__or__` overrides method defined in `type` inconsistently.
# pyre-fixme[15]: `__or__` overrides method defined in `type` inconsistently.
def __or__(
self, other: _OtherNodeT
) -> "OneOf[Union[MatchMetadataIfTrue, _OtherNodeT]]":
# Without the cast, pyre doesn't know this is valid
return cast(OneOf[Union[MatchMetadataIfTrue, _OtherNodeT]], OneOf(self, other))
return OneOf(self, other)

def __and__(
self, other: _OtherNodeT
) -> "AllOf[Union[MatchMetadataIfTrue, _OtherNodeT]]":
# Without the cast, pyre doesn't know this is valid
return cast(AllOf[Union[MatchMetadataIfTrue, _OtherNodeT]], AllOf(self, other))
return AllOf(self, other)

def __invert__(self) -> "MatchMetadataIfTrue":
# Construct a wrapped version of MatchMetadataIfTrue for typing simplicity.
Expand Down Expand Up @@ -817,7 +782,6 @@ def matcher(self) -> Union[_MatcherT, DoNotCareSentinel]:
"""
return self._matcher

# pyre-fixme[14]: `__or__` overrides method defined in `type` inconsistently.
# pyre-fixme[15]: `__or__` overrides method defined in `type` inconsistently.
def __or__(self, other: object) -> NoReturn:
raise Exception("AtLeastN cannot be used in a OneOf matcher")
Expand Down Expand Up @@ -921,7 +885,6 @@ def matcher(self) -> Union[_MatcherT, DoNotCareSentinel]:
"""
return self._matcher

# pyre-fixme[14]: `__or__` overrides method defined in `type` inconsistently.
# pyre-fixme[15]: `__or__` overrides method defined in `type` inconsistently.
def __or__(self, other: object) -> NoReturn:
raise Exception("AtMostN cannot be used in a OneOf matcher")
Expand Down
5 changes: 0 additions & 5 deletions libcst/matchers/_visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ def _verify_return_annotation(
# it is "None".
if type_hints.get("return", type(None)) is not type(None): # noqa: E721
raise MatchDecoratorMismatch(
# pyre-fixme[16]: Anonymous callable has no attribute `__qualname__`.
meth.__qualname__,
f"@{decorator_name} should only decorate functions that do "
+ "not return.",
Expand Down Expand Up @@ -181,7 +180,6 @@ def _verify_parameter_annotations(
meth_signature = signature(meth)
if len(meth_signature.parameters) != expected_param_count:
raise MatchDecoratorMismatch(
# pyre-fixme[16]: Anonymous callable has no attribute `__qualname__`.
meth.__qualname__,
f"@{decorator_name} should decorate functions which take "
+ f"{expected_param_count} parameter"
Expand Down Expand Up @@ -238,8 +236,6 @@ def _check_types(
# First thing first, make sure this isn't wrapping an inner class.
if not ismethod(meth):
raise MatchDecoratorMismatch(
# pyre-fixme[16]: Anonymous callable has no attribute
# `__qualname__`.
meth.__qualname__,
"Matcher decorators should only be used on methods of "
+ "MatcherDecoratableTransformer or "
Expand Down Expand Up @@ -292,7 +288,6 @@ def _assert_not_concrete(
) -> None:
if func.__name__ in CONCRETE_METHODS:
raise MatchDecoratorMismatch(
# pyre-ignore This anonymous method has a qualname.
func.__qualname__,
f"@{decorator_name} should not decorate functions that are concrete "
+ "visit or leave methods.",
Expand Down
30 changes: 2 additions & 28 deletions libcst/tests/pyre/simple_class.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
}
},
{
"annotation": "None",
"annotation": "typing.Type[None]",
"location": {
"start": {
"column": 34,
Expand Down Expand Up @@ -208,19 +208,6 @@
}
}
},
{
"annotation": "BoundMethod[typing.Callable(typing.GenericMeta.__getitem__)[[Named(self, unknown), typing.Type[Variable[typing._T_co](covariant)]], typing.Type[typing.Sequence[Variable[typing._T_co](covariant)]]], typing.Type[typing.Sequence]]",
"location": {
"start": {
"column": 35,
"line": 16
},
"stop": {
"column": 43,
"line": 16
}
}
},
{
"annotation": "typing.Type[typing.Sequence[simple_class.Item]]",
"location": {
Expand All @@ -234,19 +221,6 @@
}
}
},
{
"annotation": "typing.Type[simple_class.Item]",
"location": {
"start": {
"column": 44,
"line": 16
},
"stop": {
"column": 48,
"line": 16
}
}
},
{
"annotation": "typing.List[simple_class.Item]",
"location": {
Expand Down Expand Up @@ -469,7 +443,7 @@
}
},
{
"annotation": "typing.Sequence[simple_class.Item]",
"annotation": "simple_class.Item",
"location": {
"start": {
"column": 12,
Expand Down
4 changes: 4 additions & 0 deletions libcst/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def _is_whitespace(field: "dataclasses.Field[object]") -> bool:

def _get_default(fld: "dataclasses.Field[object]") -> object:
if fld.default_factory is not dataclasses.MISSING:
# pyre-fixme[29]: `Union[dataclasses._MISSING_TYPE,
# dataclasses._DefaultFactory[object]]` is not a function.
return fld.default_factory()
return fld.default

Expand Down Expand Up @@ -783,6 +785,8 @@ def _list_impl(proc_name: str, command_args: List[str]) -> int: # noqa: C901
continue

# Grab the path, try to import all of the files inside of it.
# pyre-fixme[6]: For 1st argument expected `PathLike[Variable[AnyStr <:
# [str, bytes]]]` but got `Optional[str]`.
path = os.path.dirname(os.path.abspath(imported_module.__file__))
for name, imported_module in _recursive_find(path, module):
for objname in dir(imported_module):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dev = [
"maturin>=0.8.3,<0.16",
"nbsphinx>=0.4.2",
"prompt-toolkit>=2.0.9",
"pyre-check==0.9.10; platform_system != 'Windows'",
"pyre-check==0.9.18; platform_system != 'Windows'",
"setuptools_scm>=6.0.1",
"sphinx-rtd-theme>=0.4.3",
"ufmt==2.2.0",
Expand Down

0 comments on commit 14df046

Please sign in to comment.