-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Various stubgen regressions in mypy 1.7.0 #16486
Comments
It also looks to me like If I copy the contents of I've also checked out the mypy repo and ran Line 27 in 0699dde
This |
The MR that was merged prior to 1.7 unified the code and many of the behaviors of the C-extension and pure-python stubgen processes. In the pure-python code path, imports are only added for modules that have been imported: there is limited specialized behavior to detect and import types whose names match those in the known_imports = {
"_typeshed": ["Incomplete"],
"typing": ["Any", "TypeVar", "NamedTuple"],
"collections.abc": ["Generator"],
"typing_extensions": ["TypedDict", "ParamSpec", "TypeVarTuple"],
} There are real-world projects that I use stubgen on (PySide2) which have classes that match the names of those in the typing module, and the automatic generation of imports broke the stubs. The simple solution to the problem is for docstrings to use native types (
This is another example of the C-extension behavior being adjusted to match the pure-python behavior (by sharing code paths). The C-extension behavior now correctly distinguishes types that are known to be It would be reasonable to add a flag to use def func_returning_array(*args: Any, **kwargs: Any) -> Any: ...
I'll add these to the pybind11 demo and fix them in the generator. |
This addresses several regressions identified in #16486 The primary regression from #15770 is that pybind11 properties with docstrings were erroneously assigned `typeshed. Incomplete`. The reason for the regression is that as of the introduction of the `--include-docstring` feature (#13284, not my PR, ftr), `./misc/test-stubgenc.sh` began always reporting success. That has been fixed. It was also pointed out that `--include-docstring` does not work for C-extensions. This was not actually a regression as it turns out this feature was never implemented for C-extensions (though the tests suggested it had been), but luckily my efforts to unify the pure-python and C-extension code-paths made fixing this super easy (barely an inconvenience)! So that is working now. I added back the extended list of `typing` objects that generate implicit imports for the inspection-based stub generator. I originally removed these because I encountered an issue generating stubs for `PySide2` (and another internal library) where there was an object with the same name as one of the `typing` objects and the auto-import created broken stubs. I felt somewhat justified in this decision as there was a straightforward solution -- e.g. use `list` or `typing.List` instead of `List`. That said, I recognize that the problem that I encountered is more niche than the general desire to add import statements for typing objects, so I've changed the behavior back for now, with the intention to eventually add a flag to control this behavior.
I just had time to re-check the behavior with mypy 1.8 that includes the fix by @chadrik. The example now produces the follow, which looks pretty: from typing import List, Optional, Tuple
class FieldAnnotationTest:
a: int
b: int
def __init__(self, *args, **kwargs) -> None: ...
@property
def c(self) -> int: ...
def answer() -> int: ...
def func_incomplete_signature(*args, **kwargs): ...
def func_returning_optional() -> Optional[int]: ...
def func_returning_pair() -> Tuple[int, float]: ...
def func_returning_vector() -> List[float]: ... There is only the very minor regression left that the signature of the @chadrik Thanks again for the fix! |
Improve test cases around #16486 This PR does not change any actual mypy behavior, only hardens the stubgen tests. The specific changes are: - **dedicated test cases**: The existing pybind11 test cases originate from a pybind11 demo. They cover a specific topic involving geometry types and semi-implemented logic related to it. This somehow distracts from the aspects we are trying to test here from the mypy stubgen perspective, because it hides the actual intent of the bindings. I've simply started adding new test cases that clearly express via their name what the test case is addressing. I've kept the original demo stuff for now, so that the new cases are just an addition (overhead is negligible). - **running mypy on the generated stubs**: In general it is crucial that the output produced by the stubgen can actually be type checked by mypy (this was the regression in #18486). This wasn't covered by the CI check so far. I've added check now, which would have avoided the regression. My goal for follow up PRs would be that we can use `mypy --disallow-untyped-defs` or even `mypy --strict` on the output. - **minor directory restructuring**: So far the expected stub outputs were stored in folder names `stubgen` and `stubgen-include-docs`. This was a bit confusing, because the folder `stubgen` suggested it contains _the_ stubgen (implementation). I went for `expected_stubs_no_docs` and `expected_stubs_with_docs` to make the role of the two folders more explicit. - **minor script bugfix**: Fix a bug in `test-stubgen.sh`: The pre-delete functionality was broken, because the `*` was quoted and therefore did not match.
Bug Report
Trying to update mypy from 1.6.1 to 1.7.0 has revealed a few regressions in the stub generator output related to pybind11 modules.
To Reproduce
Consider this pybind11 module:
To reproduce, store the above snippet as
my_native_module.cpp
and then:Expected Behavior
mypy 1.6.1 outputs the following, which is pretty reasonable:
Actual Behavior
mypy 1.7.0 outputs the following:
The regressions are:
List
,Tuple
, andOptional
are missing. This makes the resulting.pyi
invalid, because it uses symbols that have not been imported. The stub output does not type check. This is not limited to these three types, but applies to other types likeIterable
,Iterator
,Dict
,Set
,Union
, etc.b
has regressed fromint
toIncomplete
, apparently due to the explicit docstring.c
has regressed fromint
to not annotated at all, probably also because of the explicit docstring.func_incomplete_signature
has also slightly worsened: Before, it was at least annotated as-> Any
but now it misses any return type annotation at all. This is problematic when users want to validate their generated stubs for completeness, i.e., runmypy --disallow-untyped-defs
on the output. Of course semantically it is the same, but still it would be nicer to always produce some return type (arguably a broken signature may be rather annotated as-> object
instead of-> Incomplete
to avoid running into bugs from incomplete stub output).Your Environment
mypy.ini
(and other config files): irrelevant for stubgenThe text was updated successfully, but these errors were encountered: