-
Notifications
You must be signed in to change notification settings - Fork 242
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
Third-party stubs: recommending a default path for installing stub files, overriding stubs #84
Comments
Ended up recommending shared/typehints/python3.5, etc. since:
@JukkaL, I've seen https://github.com/JukkaL/mypy/tree/master/stubs has a similar concept with distinct stubs per Python version. Do you see any problem with the suggestion? |
My plan is actually to get rid of the separate stub directories for Python 3.4 etc. in mypy. The reason is that it makes stubs a more difficult to maintain with marginal benefits. Having separate stubs for Python 2 and 3 would be useful, however, since they are often significantly different. My plan is to only have Also, should the directory be Potentially we could recommend something like Also, we discussed the possibility of having a single stub file that works in all Python versions (2.x and 3.x). It would be nice if we didn't have to maintain two copies of such a stub file, as these could easily get out of sync. |
You could do something like the following which allows you to add a more specific stub file for a specific Python version
Which, with the following file structure
prints
|
@JukkaL, so if we want to use PyPI and
Moreover, as we talked about this with @vlasovskikh, if a construct is described in the stubs, we trust that it is correct. For instance, if functools.pyi has |
I can't tell from this discussion if this requires PEP changes or not. I am labeling this as enhancement which I will interpret as "in the future, maybe", i.e. "no need to change the PEP or typing.py now". |
(Sorry, didn't mean to close.) |
FYI, I have thought a lot about this, and I think the best path forward is to extend the Currently mypy's stubs DTWT for python 3.2/3.3, because the stubs for modules that were present in 3.2 include functions that were only added in 3.4. And there are even cases where functions were removed in later python versions. |
Actually, Mark Shannon made me remove typing.PY3 and other platform checks. Instead, type checkers should learn how typical programs check for platforms. So extending typing.PY3 is not an option. (I think this issue is actually about something else, so I won't close it.) |
In that case, a single stub file can write You're right that it's not the direct focus of this PR, but the choosing in-file vs out-of-file multiversioning will affect the answer for what the paths should be. That said, I am not looking forward to implementing the "calculate what |
I'm interested in moving this issue forward, because it appears to be a somewhat common problem for mypy users that there is no standard place to install third-party stubs outside of typeshed. You can manually install stubs into some directory and set $MYPYPATH, but that is fragile and not portable. By fixing this issue, we could also fix python/typeshed#153, because third-party modules could now come with version-specific stubs. I think Łukasz's approach of installing stubs using
Other type checkers may provide additional implementation-specific ways to find the shared stub directory. If we go with this approach, how should it be codified? I could write a new PEP, but perhaps this is small enough that it can just go as a new section into PEP 484. I have a proof-of-concept implementation in https://github.com/JelleZijlstra/mypy/tree/stubdir and a library using the functionality at https://github.com/JelleZijlstra/sqlalchemy-stubs. |
Using functionality proposed in python/typing#84
I like this approach, but I'd propose using the same naming scheme for |
The PEP 484 text on this issue is pretty vague, and I propose to write a separate PEP, just so the design is clear and we can easily get feedback from people maintaining various libraries or using various platforms. Can you start a PEP for this purpose? |
Yes, I'll do that. |
Actually, I just re-read PEP 484, and at https://www.python.org/dev/peps/pep-0484/#storing-and-distributing-stub-files it has pretty much the same specification that's being proposed here. However, that text seems to implicitly assume type checkers run the same Python version they're checking, which is not currently true. So there's still value in a new PEP to specify in more detail how stub files should be distributed. I'm going to work on the new PEP at https://github.com/JelleZijlstra/peps/tree/distributingstubs. I'll post here again to ask for feedback when I'm happy with the PEP text as written, but in the meantime I'm open to any suggestions on what the PEP should cover and how the system should work. |
Great! I feel that the current text in the PEP falls short in giving an algorithm for a type checker for finding stubs, even given a Python executable (to which it can feed arbitrary code, within reason). The PEP currently defers to PYTHONPATH (and where is the |
This is something I think would be really important to have! I was considering writing up a proposal myself. If you need help with the PEP, I'd be happy to help in reviewing or any other way I can! |
Mypy documentation is currently warning against using site-packages with However, the more I think about it in context of shipping annotated libraries or .pyi files for user consumption, the more I think we should just use site-packages. Why?
There are three issues with this that I can identify:
The first two can be solved by teaching the type checker that errors in site-packages should be silenced by default unless really asked for. Essentially a less hacky form of this: As an option we can consider also ignoring .py files altogether if they don't contain a single type annotation/type comment. The size aspect of a library installing .pyi files is a nit. Embedded type annotations already have weight, so moving them to a separate .pyi file is a miniscule cost. If the author of a given library really cares about every last byte, we can recommend putting typing as a separate extra feature in |
Mypy has a feature I think it would also be good to be able to point a type checker to a particular module in site-packages without implying that site-packages itself should go on the search path (this is probably one of the less-understood features of mypy's search path -- for any file or folder specified on the command line it implicitly adds the containing folder to the search path, before MYPYPATH). |
I agree that people should be able to point to a particular module in site packages, instead of all of it. Im concerned if we start trying to deal with files that are untyped and not verified against Mypy it may crash, which would obviously be a problem, as the user would have to uninstall or modify the installed library, which isn't a great experience either way. |
Yes, for example I had a spurious install of typing.py in my site-packages and that totally crashed mypy. I propose a concrete test case: there are stubs in typeshed for werkzeug and jinja2 but not for flask. It should be possible to typecheck site-packages/flask while using the typeshed stubs for werkzeug and jinja2. Currently the only way I can think of making that work is making a copy of the flask package (or a symlink or take the source tree) and point mypy at that -- but I think it should be possible to just do it using |
I don't think versioning for stub libraries is an issue: user should install them by Though we could recommend supporting the same version number by stub library authors, e.g. for Django==1.11 django-stub should be 1.11 too. |
But what if the author django-stub discovers that they made a mistake in their stubs for Django 1.11? Perhaps they could release django-stub 1.11.1, but then that would mean they'd have to maintain separate, mostly identical copies of the stubs for each Django version. I think we'll have to support something like this straw man: stubs can do |
Yeah, I think this proposal supports library versioning well enough. [Clarification: This was written before Jelle's comment above, in response to @asvetlov's "I don't think versioning for stub libraries is an issue".] |
This feels like worrying too much. We've never even tried any of this proposal, and we don't know if this scenario will occur frequently enough to worry about. But if we specify something to handle this now we'll never be able to remove that feature, no matter how unpopular (because it'll always break someone's workflow). Also, I really don't like having to support library version comparisons in the checker -- unlike Python versions we don't have control over the ordering of libraries (they don't all use semver). The maintenance problem can be solved using branches. We're better off allowing some freedom in the naming of stub packages -- maybe the package name will end up including the django version (e.g. django-1.1-stubs) so the package version can be whatever the stub package author wants. |
This is the recommended approach for 3rd party stubs. See: python/typing#84 (comment)
I've started writing up a PEP. Hopefully I can get a draft out tomorrow or the next day. Things to think about which I leave open for discussion (because they should be discussed more):
|
|
Okay, I have a rough draft here: https://github.com/ethanhs/peps/blob/typedist/pep-0561.rst I also added a PR python/peps#415 if people prefer the Github review UI, but also feel free to leave comments in this issue. I plan on making a POC implementation of the distutils extension when I have time either later today or tomorrow. A couple points Im not sure about: should the keyword be a boolean about whether the package is stubbed or not? Then Mixed inline/stubbed packages - I originally wrote a version with an additional option for this, but it seemed to make things more complicated than needed. Thanks! |
This is the recommended approach for 3rd party stubs. See: python/typing#84 (comment)
The PEP has been posted to Python-dev and the latest live version can be found here: https://www.python.org/dev/peps/pep-0561/ |
This is the recommended approach for 3rd party stubs. See: python/typing#84 (comment)
* Add PEP484 stubs * Deploy .pyi stubs alongside .py files. This is the recommended approach for 3rd party stubs. See: python/typing#84 (comment) * Add support for the new type argument. * Add tests for stubs and address a few issues. * Improve declaration of private vs public objects in stubs * More stub tests * Separate the stub tests into their own tox env it does not make sense to test the stubs in multiple python *runtime* environments (e.g. python 3.5, 3.6, pypy3) because the results of static analysis wrt attrs is not dependent on the runtime. Moreover, mypy is not installing correctly in pypy3 which has nothing to do with attrs. * Update the manifest with stub files * Remove mypy from the dev requirements * Allow _CountingAttr to be instantiated, but not Attribute. * Incorporate defaults into attr.ib typing * Fix a bug with validators.and_ * Add more tests * Remove _CountingAttr from public interface It is crucial to ensure that make_class() works with attr.ib(), as a result we no longer have any functions that care about _CountingAttr. * Lie about return type of Factory this allows for an abbreviated idiom: `x: List[int] = Factory(list)` * Add tox stubs env to travis * used the wrong comment character in mypy tests * Improve overloads using PyCharm order-based approach overloads are pretty broken in mypy. the best we can do for now is target PyCharm, which is much more forgiving. * Remove features not yet working in mypy. Document remaining issues. * Test stubs against euresti fork of mypy with attrs plugin Copied the pytest plugin from mypy for testing annotations: It is not an officially supported API and using the plugin from mypy could break after any update. * Add some types and TypeVars to some types. Make tests pass * Suppress warnings about named attribute access from fields() e.g. fields(C).x Eventually it would be good to add support for returning NamedTuple from the mypy plugin * Add WIP mypy-doctest plugin * Deal with a few remaining type issues in the docs * sphinx doctest: don't turn warnings into errors. doing so makes the tests abort after the first failed group. * Update "type: ignore" comments to reflect issues fixed in mypy plugin * doctest2: improve output formatting * Update manifest * static tests: use inline error declarations * More tests * Tests passing (with notes about remaining issues) * Attempt to get latest plugin from euresti working * Issues fixed. Had to place calls to attr.ib under a class definition. * Deal with a PyCharm bug * Minor test improvements * Make tests prettier * Use 2 decorators instead of 3 * doctest2: add support for skipping mypy tests * Add tests for inheritance, eq, and cmp * Add fixmes and todos * Rename convert to converter * Attribute.validator is always a single validator * Conform stubs to typeshed coding style And add auto_attrib kw * backport style fixes from typeshed * Add test cases to cover forward references and Any * Add fixes for forward references and Any * Address typeshed review notes * Use Sequence instead of List/Tuple for validator arg list and tuple are invariant and so prevent passing subtypes of _ValidatorType * backports changes from typeshed #1914 * backport changes from typeshed #1933 * Prevent mypy tests from getting picked up Evidently the discovery rules changed recently for pytest. * make our doctest extension compatible with latest sphinx * Adjustments to the tests * Fix flake and manifest tests (hopefully) * Fix tests on pypy3 (hopefully) * Update stubs from typeshed Also update tests. * Make PEP 561-compliant * minor cleanup * Consolidate stub support files into stub directory In preparation for removing them from the pyi_stubs branch. * Get tests passing This is a final test of the current stubs before moving the stub tests to a new branch. * Revert stub test additions Replace with a simple mypy pass/fail test * get pre-commit passing * Address review feedback * Move typing test up in tox envlist
I believe PEP 561 resolves this issue, now that it is accepted. |
Yes! |
It was a long way. Thanks @ethanhs for all the work on this! |
The ideal situation is where a file is annotated. The other obvious choice if there is a module.pyi stub file alongside module.py. However, since package maintainers are free not to add type hinting to their packages, there has to be support for third-party stubs installable by
pip
from PyPI. This opens the following questions:data_files=('share/typehinting/', pathlib.Path(SRC_PATH).glob('**/*.pyi'))
in setup.py. We are proposing to add a setup.cfg hook to do the right thing automatically."shared/typehinting/
) where third-party packages can put their implementation of stubs for external packages. Also, tools like type checkers or IDEs might provide their own custom paths for stubs they themselves populate, etc. The type checker itself is expected to only load one *.pyi file per corresponding *.py module.The text was updated successfully, but these errors were encountered: