Skip to content

Commit

Permalink
Merge branch 'master' into mypybot/sync-typeshed
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja authored Nov 11, 2022
2 parents 4c48c2b + 67855fa commit 67e2933
Show file tree
Hide file tree
Showing 59 changed files with 896 additions and 350 deletions.
7 changes: 4 additions & 3 deletions docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,10 @@ potentially problematic or redundant in some way.
are when:

- The function has a ``None`` or ``Any`` return type
- The function has an empty body or a body that is just
ellipsis (``...``). Empty functions are often used for
abstract methods.
- The function has an empty body and is marked as an abstract method,
is in a protocol class, or is in a stub file
- The execution path can never return; for example, if an exception
is always raised

Passing in :option:`--no-warn-no-return` will disable these error
messages in all cases.
Expand Down
46 changes: 15 additions & 31 deletions docs/source/running_mypy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -322,34 +322,27 @@ the library, you will get a message like this:
main.py:1: note: Hint: "python3 -m pip install types-PyYAML"
main.py:1: note: (or run "mypy --install-types" to install all missing stub packages)
You can resolve the issue by running the suggested pip command or
commands. Alternatively, you can use :option:`--install-types <mypy
--install-types>` to install all known missing stubs:
You can resolve the issue by running the suggested pip commands.
If you're running mypy in CI, you can ensure the presence of any stub packages
you need the same as you would any other test dependency, e.g. by adding them to
the appropriate ``requirements.txt`` file.

.. code-block:: text
mypy --install-types
This installs any stub packages that were suggested in the previous
mypy run. You can also use your normal mypy command line with the
extra :option:`--install-types <mypy --install-types>` option to
install missing stubs at the end of the run (if any were found).

Use :option:`--install-types <mypy --install-types>` with
:option:`--non-interactive <mypy --non-interactive>` to install all suggested
stub packages without asking for confirmation, *and* type check your
code, in a single command:
Alternatively, add the :option:`--install-types <mypy --install-types>`
to your mypy command to install all known missing stubs:

.. code-block:: text
mypy --install-types --non-interactive src/
mypy --install-types
This can be useful in Continuous Integration jobs if you'd prefer not
to manage stub packages manually. This is somewhat slower than
explicitly installing stubs before running mypy, since it may type
check your code twice -- the first time to find the missing stubs, and
This is slower than explicitly installing stubs, since it effectively
runs mypy twice -- the first time to find the missing stubs, and
the second time to type check your code properly after mypy has
installed the stubs.
installed the stubs. It also can make controlling stub versions harder,
resulting in less reproducible type checking.

By default, :option:`--install-types <mypy --install-types>` shows a confirmation prompt.
Use :option:`--non-interactive <mypy --non-interactive>` to install all suggested
stub packages without asking for confirmation *and* type check your code:

If you've already installed the relevant third-party libraries in an environment
other than the one mypy is running in, you can use :option:`--python-executable
Expand Down Expand Up @@ -394,15 +387,6 @@ this error, try:
you must run ``mypy ~/foo-project/src`` (or set the ``MYPYPATH`` to
``~/foo-project/src``.

In some rare cases, you may get the "Cannot find implementation or library
stub for module" error even when the module is installed in your system.
This can happen when the module is both missing type hints and is installed
on your system in an unconventional way.

In this case, follow the steps above on how to handle
:ref:`missing type hints in third party libraries <missing-type-hints-for-third-party-library>`.


.. _finding-imports:

How imports are found
Expand Down
9 changes: 4 additions & 5 deletions misc/fix_annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ def has_return_exprs(self, node):
results = {}
if self.return_expr.match(node, results):
return True
for child in node.children:
if child.type not in (syms.funcdef, syms.classdef):
if self.has_return_exprs(child):
return True
return False
return any(
child.type not in (syms.funcdef, syms.classdef) and self.has_return_exprs(child)
for child in node.children
)
19 changes: 17 additions & 2 deletions misc/upload-pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ def is_whl_or_tar(name: str) -> bool:
return name.endswith(".tar.gz") or name.endswith(".whl")


def item_ok_for_pypi(name: str) -> bool:
if not is_whl_or_tar(name):
return False

if name.endswith(".tar.gz"):
name = name[:-7]
if name.endswith(".whl"):
name = name[:-4]

if name.endswith("wasm32"):
return False

return True


def get_release_for_tag(tag: str) -> dict[str, Any]:
with urlopen(f"{BASE}/{REPO}/releases/tags/{tag}") as f:
data = json.load(f)
Expand Down Expand Up @@ -75,7 +90,7 @@ def check_sdist(dist: Path, version: str) -> None:


def spot_check_dist(dist: Path, version: str) -> None:
items = [item for item in dist.iterdir() if is_whl_or_tar(item.name)]
items = [item for item in dist.iterdir() if item_ok_for_pypi(item.name)]
assert len(items) > 10
assert all(version in item.name for item in items)
assert any(item.name.endswith("py3-none-any.whl") for item in items)
Expand All @@ -93,7 +108,7 @@ def tmp_twine() -> Iterator[Path]:

def upload_dist(dist: Path, dry_run: bool = True) -> None:
with tmp_twine() as twine:
files = [item for item in dist.iterdir() if is_whl_or_tar(item.name)]
files = [item for item in dist.iterdir() if item_ok_for_pypi(item.name)]
cmd: list[Any] = [twine, "upload"]
cmd += files
if dry_run:
Expand Down
30 changes: 16 additions & 14 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,7 @@ def __init__(
raise
if follow_imports == "silent":
self.ignore_all = True
elif path and is_silent_import_module(manager, path):
elif path and is_silent_import_module(manager, path) and not root_source:
self.ignore_all = True
self.path = path
if path:
Expand Down Expand Up @@ -2629,7 +2629,7 @@ def find_module_and_diagnose(
else:
skipping_module(manager, caller_line, caller_state, id, result)
raise ModuleNotFound
if is_silent_import_module(manager, result):
if is_silent_import_module(manager, result) and not root_source:
follow_imports = "silent"
return (result, follow_imports)
else:
Expand Down Expand Up @@ -2728,11 +2728,8 @@ def in_partial_package(id: str, manager: BuildManager) -> bool:
else:
parent_mod = parent_st.tree
if parent_mod is not None:
if parent_mod.is_partial_stub_package:
return True
else:
# Bail out soon, complete subpackage found
return False
# Bail out soon, complete subpackage found
return parent_mod.is_partial_stub_package
id = parent
return False

Expand Down Expand Up @@ -3024,7 +3021,11 @@ def load_graph(
for bs in sources:
try:
st = State(
id=bs.module, path=bs.path, source=bs.text, manager=manager, root_source=True
id=bs.module,
path=bs.path,
source=bs.text,
manager=manager,
root_source=not bs.followed,
)
except ModuleNotFound:
continue
Expand Down Expand Up @@ -3576,9 +3577,10 @@ def record_missing_stub_packages(cache_dir: str, missing_stub_packages: set[str]


def is_silent_import_module(manager: BuildManager, path: str) -> bool:
if not manager.options.no_silence_site_packages:
for dir in manager.search_paths.package_path + manager.search_paths.typeshed_path:
if is_sub_path(path, dir):
# Silence errors in site-package dirs and typeshed
return True
return False
if manager.options.no_silence_site_packages:
return False
# Silence errors in site-package dirs and typeshed
return any(
is_sub_path(path, dir)
for dir in manager.search_paths.package_path + manager.search_paths.typeshed_path
)
Loading

0 comments on commit 67e2933

Please sign in to comment.