From 738dc2f893da69150d71e964831e7b6e594b86fb Mon Sep 17 00:00:00 2001 From: Zsolt Dollenstein Date: Mon, 2 Oct 2023 09:43:17 -0700 Subject: [PATCH] Upgrade pyre (#1032) * Upgrade pyre * regen fixtures --- libcst/_nodes/module.py | 1 - .../codemod/commands/convert_type_comments.py | 6 +- libcst/matchers/_matcher_base.py | 61 ++++--------------- libcst/matchers/_visitors.py | 5 -- libcst/tests/pyre/simple_class.json | 30 +-------- libcst/tool.py | 4 ++ pyproject.toml | 2 +- 7 files changed, 20 insertions(+), 89 deletions(-) diff --git a/libcst/_nodes/module.py b/libcst/_nodes/module.py index 149a4375b..9ed45716a 100644 --- a/libcst/_nodes/module.py +++ b/libcst/_nodes/module.py @@ -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. diff --git a/libcst/codemod/commands/convert_type_comments.py b/libcst/codemod/commands/convert_type_comments.py index 8335160e9..e2c6e71cc 100644 --- a/libcst/codemod/commands/convert_type_comments.py +++ b/libcst/codemod/commands/convert_type_comments.py @@ -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, "", "eval").body except SyntaxError: return None @@ -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"), - ) + return ast.parse(func_type_comment, "", "func_type") @functools.lru_cache() diff --git a/libcst/matchers/_matcher_base.py b/libcst/matchers/_matcher_base.py index 666ceab03..039694a5f 100644 --- a/libcst/matchers/_matcher_base.py +++ b/libcst/matchers/_matcher_base.py @@ -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) @@ -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)) @@ -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 @@ -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])})" @@ -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])})" @@ -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] @@ -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] @@ -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. @@ -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 @@ -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 @@ -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. @@ -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") @@ -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") diff --git a/libcst/matchers/_visitors.py b/libcst/matchers/_visitors.py index a491ffd10..9349c5b53 100644 --- a/libcst/matchers/_visitors.py +++ b/libcst/matchers/_visitors.py @@ -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.", @@ -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" @@ -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 " @@ -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.", diff --git a/libcst/tests/pyre/simple_class.json b/libcst/tests/pyre/simple_class.json index 878ed5ebb..851925591 100644 --- a/libcst/tests/pyre/simple_class.json +++ b/libcst/tests/pyre/simple_class.json @@ -79,7 +79,7 @@ } }, { - "annotation": "None", + "annotation": "typing.Type[None]", "location": { "start": { "column": 34, @@ -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": { @@ -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": { @@ -469,7 +443,7 @@ } }, { - "annotation": "typing.Sequence[simple_class.Item]", + "annotation": "simple_class.Item", "location": { "start": { "column": 12, diff --git a/libcst/tool.py b/libcst/tool.py index 5aa4d12f5..64144cba1 100644 --- a/libcst/tool.py +++ b/libcst/tool.py @@ -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 @@ -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): diff --git a/pyproject.toml b/pyproject.toml index ced625528..c3bb9bda8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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",