-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Implement PEP 561 searching #4403
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 all commits
e4b5274
6b6a97d
2c936e1
8f64f81
ddc526f
329dc68
eba98d4
5576629
2397763
fda2a2c
59f0c49
c976008
8df8b8d
4ca6939
a96601c
336fb6b
8492d8b
c41eef2
1f005d7
45afe58
e7f23bb
d74eae8
5822884
c734bc2
1d7cc7e
1ac4253
f681bf9
85f233c
3726f18
59a2004
77b3b66
1da6e58
e54025f
056dc8f
297fac6
e35e7aa
ecb702e
1a41262
31dab8d
ff15057
9442765
a34e7e8
0fd592a
4cbe21a
f566ed0
206f70b
2dbff97
e33ceb7
644e9a5
af38a12
b7f0788
0564a5c
d17582b
085d002
8958dc7
84fd527
4c0ff1b
a917ff5
83ab973
cfac326
aefcb96
380e301
47d8e0d
5c19e8c
56dfcd4
4293dad
b810d3e
17488ad
e348471
8184937
97f29cc
928683a
ae65564
f6c4327
d475d4b
03cd378
3a8ca60
e1f9494
b5cbd09
fc58e4c
96e2f55
80f4a35
dc29e51
be733c5
c1264a0
ceacbff
f389f64
1bd9f66
806fbef
c9e3361
7be3555
5fc9d52
ae86abd
0bf024f
c9c35c1
dca9c54
a929b46
e56ffe3
72d7b0a
c9f6bc2
4698d23
db0680a
8367e57
24b6742
afb80ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,17 +8,21 @@ summary of command line flags can always be printed using the ``-h`` | |
flag (or its long form ``--help``):: | ||
|
||
$ mypy -h | ||
usage: mypy [-h] [-v] [-V] [--python-version x.y] [--platform PLATFORM] [-2] | ||
[--ignore-missing-imports] | ||
usage: mypy [-h] [-v] [-V] [--python-version x.y] | ||
[--python-executable PYTHON_EXECUTABLE] [--platform PLATFORM] [-2] | ||
[--ignore-missing-imports] [--no-site-packages] | ||
[--follow-imports {normal,silent,skip,error}] | ||
[--disallow-any-{unimported,expr,decorated,explicit,generics}] | ||
[--disallow-untyped-calls] [--disallow-untyped-defs] | ||
[--disallow-any-unimported] [--disallow-any-expr] | ||
[--disallow-any-decorated] [--disallow-any-explicit] | ||
[--disallow-any-generics] [--disallow-untyped-calls] | ||
[--disallow-untyped-defs] [--disallow-incomplete-defs] | ||
[--check-untyped-defs] [--disallow-subclassing-any] | ||
[--warn-incomplete-stub] [--warn-redundant-casts] | ||
[--no-warn-no-return] [--warn-return-any] [--warn-unused-ignores] | ||
[--warn-incomplete-stub] [--disallow-untyped-decorators] | ||
[--warn-redundant-casts] [--no-warn-no-return] [--warn-return-any] | ||
[--warn-unused-ignores] [--warn-unused-configs] | ||
[--show-error-context] [--no-implicit-optional] [-i] | ||
[--quick-and-dirty] [--cache-dir DIR] [--skip-version-check] | ||
[--strict-optional] | ||
[--quick-and-dirty] [--cache-dir DIR] [--cache-fine-grained] | ||
[--skip-version-check] [--strict-optional] | ||
[--strict-optional-whitelist [GLOB [GLOB ...]]] | ||
[--junit-xml JUNIT_XML] [--pdb] [--show-traceback] [--stats] | ||
[--inferstats] [--custom-typing MODULE] | ||
|
@@ -28,9 +32,9 @@ flag (or its long form ``--help``):: | |
[--shadow-file SOURCE_FILE SHADOW_FILE] [--any-exprs-report DIR] | ||
[--cobertura-xml-report DIR] [--html-report DIR] | ||
[--linecount-report DIR] [--linecoverage-report DIR] | ||
[--memory-xml-report DIR] | ||
[--txt-report DIR] [--xml-report DIR] [--xslt-html-report DIR] | ||
[--xslt-txt-report DIR] [-m MODULE] [-c PROGRAM_TEXT] [-p PACKAGE] | ||
[--memory-xml-report DIR] [--txt-report DIR] [--xml-report DIR] | ||
[--xslt-html-report DIR] [--xslt-txt-report DIR] [-m MODULE] | ||
[-c PROGRAM_TEXT] [-p PACKAGE] | ||
[files [files ...]] | ||
|
||
(etc., too long to show everything here) | ||
|
@@ -366,11 +370,29 @@ Here are some more useful flags: | |
updates the cache, but regular incremental mode ignores cache files | ||
written by quick mode. | ||
|
||
- ``--python-executable EXECUTABLE`` will have mypy collect type information | ||
from `PEP 561`_ compliant | ||
packages installed for the Python executable ``EXECUTABLE``. If not provided, | ||
mypy will use PEP 561 compliant packages installed for the Python executable | ||
running mypy. See :ref:`installed-packages` for more on making PEP 561 | ||
compliant packages. This flag will attempt to set ``--python-version`` if not | ||
already set. | ||
|
||
- ``--python-version X.Y`` will make mypy typecheck your code as if it were | ||
run under Python version X.Y. Without this option, mypy will default to using | ||
whatever version of Python is running mypy. Note that the ``-2`` and | ||
``--py2`` flags are aliases for ``--python-version 2.7``. See | ||
:ref:`version_and_platform_checks` for more about this feature. | ||
:ref:`version_and_platform_checks` for more about this feature. This flag | ||
will attempt to find a Python executable of the corresponding version to | ||
search for `PEP 561`_ compliant | ||
packages. If you'd like to disable this, see ``--no-site-packages`` below. | ||
|
||
- ``--no-site-packages`` will disable searching for | ||
`PEP 561`_ compliant packages. | ||
This will also disable searching for a usable Python executable. Use this | ||
flag if mypy cannot find a Python executable for the version of Python being | ||
checked, and you don't need to use PEP 561 typed packages. Otherwise, use | ||
``--python-executable``. | ||
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. Does this last sentence mean "If you do need them and mypy cannot find the executable, specify it explicitly with --python-executable" 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. Yes |
||
|
||
- ``--platform PLATFORM`` will make mypy typecheck your code as if it were | ||
run under the the given operating system. Without this option, mypy will | ||
|
@@ -453,6 +475,9 @@ For the remaining flags you can read the full ``mypy -h`` output. | |
|
||
Command line flags are liable to change between releases. | ||
|
||
|
||
.. _PEP 561: https://www.python.org/dev/peps/pep-0561/ | ||
|
||
.. _integrating-mypy: | ||
|
||
Integrating mypy into another Python application | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ Mypy is a static type checker for Python. | |
command_line | ||
config_file | ||
python36 | ||
installed_packages | ||
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. Shouldn't you add 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. I realized this file is essentially a duplicate of |
||
faq | ||
cheat_sheet | ||
cheat_sheet_py3 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
.. _installed-packages: | ||
|
||
Using Installed Packages | ||
======================== | ||
|
||
`PEP 561 <https://www.python.org/dev/peps/pep-0561/>`_ specifies how to mark | ||
a package as supporting type checking. Below is a summary of how to create | ||
PEP 561 compatible packages and have mypy use them in type checking. | ||
|
||
Making PEP 561 compatible packages | ||
********************************** | ||
|
||
Packages that must be imported at runtime and supply type information should | ||
put a ``py.typed`` in their package directory. For example, with a directory | ||
structure as follows: | ||
|
||
.. code-block:: text | ||
|
||
setup.py | ||
package_a/ | ||
__init__.py | ||
lib.py | ||
py.typed | ||
|
||
the setup.py might look like: | ||
|
||
.. code-block:: python | ||
|
||
from distutils.core import setup | ||
|
||
setup( | ||
name="SuperPackageA", | ||
author="Me", | ||
version="0.1", | ||
package_data={"package_a": ["py.typed"]}, | ||
packages=["package_a"] | ||
) | ||
|
||
Some packages have a mix of stub files and runtime files. These packages also require | ||
a ``py.typed`` file. An example can be seen below: | ||
|
||
.. code-block:: text | ||
|
||
setup.py | ||
package_b/ | ||
__init__.py | ||
lib.py | ||
lib.pyi | ||
py.typed | ||
|
||
the setup.py might look like: | ||
|
||
.. code-block:: python | ||
|
||
from distutils.core import setup | ||
|
||
setup( | ||
name="SuperPackageB", | ||
author="Me", | ||
version="0.1", | ||
package_data={"package_b": ["py.typed", "lib.pyi"]}, | ||
packages=["package_b"] | ||
) | ||
|
||
In this example, both ``lib.py`` and ``lib.pyi`` exist. At runtime, ``lib.py`` | ||
will be used, however mypy will use ``lib.pyi``. | ||
|
||
If the package is stub-only (not imported at runtime), the package should have | ||
a prefix of the runtime package name and a suffix of ``-stubs``. | ||
A ``py.typed`` file is not needed for stub-only packages. For example, if we | ||
had stubs for ``package_c``, we might do the following: | ||
|
||
.. code-block:: text | ||
|
||
setup.py | ||
package_c-stubs/ | ||
__init__.pyi | ||
lib.pyi | ||
|
||
the setup.py might look like: | ||
|
||
.. code-block:: python | ||
|
||
from distutils.core import setup | ||
|
||
setup( | ||
name="SuperPackageC", | ||
author="Me", | ||
version="0.1", | ||
package_data={"package_c-stubs": ["__init__.pyi", "lib.pyi"]}, | ||
packages=["package_c-stubs"] | ||
) | ||
|
||
Using PEP 561 compatible packages with mypy | ||
******************************************* | ||
|
||
Generally, you do not need to do anything to use installed packages for the | ||
Python executable used to run mypy. They should be automatically picked up by | ||
mypy and used for type checking. | ||
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. I'm guessing that custom import hooks are not supported? 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. No. Custom import hooks are not supported by mypy normally, and this doesn't add support for that either. Do you have a specific need for that? 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. Nope, but it might be nice to note in the docs exactly where mypy stops respecting the normal import mechanics. Are zip imports supported? 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. I will add a note of that to the docs, and no, zip imports are not supported to my knowledge. 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. How do 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. See #4277 for the namespace packages implementation. That will be added later. 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. Hm, is there a dependency between this and #4277? Which should be merged first? |
||
|
||
By default, mypy searches for packages installed for the Python executable | ||
running mypy. It is highly unlikely you want this situation if you have | ||
installed typed packages in another Python's package directory. | ||
|
||
Generally, you can use the ``--python-version`` flag and mypy will try to find | ||
the correct package directory. If that fails, you can use the | ||
``--python-executable`` flag to point to the exact executable, and mypy will | ||
find packages installed for that Python executable. | ||
|
||
Note that mypy does not support some more advanced import features, such as zip | ||
imports, namespace packages, and custom import hooks. | ||
|
||
If you do not want to use typed packages, use the ``--no-site-packages`` flag | ||
to disable searching. |
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.
Where did this option come from, and if it's introduced in this patch, should it be documented too?
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.
It was introduced in #4526 but not added to the docs it seems.
I suppose it would be better to have it formally documented in a seperate PR.(Guido wants me to add it)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.
(FWIW the reason it didn't appear in the docs right away was simply that we don't regenerate this list every time we add a flag. But we should.)