Skip to content

Commit ba6a8f2

Browse files
Merge branch 'master' into pre-commit-ci-update-config
2 parents fa7101b + d94b972 commit ba6a8f2

File tree

116 files changed

+2124
-871
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+2124
-871
lines changed

.github/workflows/test_stubgenc.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Test stubgenc on pybind11-mypy-demo
1+
name: Test stubgenc on pybind11_fixtures
22

33
on:
44
workflow_dispatch:
@@ -10,6 +10,7 @@ on:
1010
- 'misc/test-stubgenc.sh'
1111
- 'mypy/stubgenc.py'
1212
- 'mypy/stubdoc.py'
13+
- 'mypy/stubutil.py'
1314
- 'test-data/stubgen/**'
1415

1516
permissions:

.pre-commit-config.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ repos:
66
- id: trailing-whitespace
77
- id: end-of-file-fixer
88
- repo: https://github.com/psf/black-pre-commit-mirror
9-
rev: 23.12.1 # must match test-requirements.txt
9+
rev: 24.1.1 # must match test-requirements.txt
1010
hooks:
1111
- id: black
12+
exclude: '^(test-data/)'
1213
- repo: https://github.com/astral-sh/ruff-pre-commit
13-
rev: v0.1.9 # must match test-requirements.txt
14+
rev: v0.1.15 # must match test-requirements.txt
1415
hooks:
1516
- id: ruff
1617
args: [--exit-non-zero-on-fix]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ To report a bug or request an enhancement:
4141

4242
To discuss a new type system feature:
4343

44-
- discuss at [typing-sig mailing list](https://mail.python.org/archives/list/typing[email protected]/)
45-
- there is also some historical discussion [here](https://github.com/python/typing/issues)
44+
- discuss at [discuss.python.org](https://discuss.python.org/c/typing/32)
45+
- there is also some historical discussion at the [typing-sig mailing list](https://mail.python.org/archives/list/[email protected]/) and the [python/typing repo](https://github.com/python/typing/issues)
4646

4747
What is mypy?
4848
-------------

docs/source/cheat_sheet_py3.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ Classes
209209
# This will allow access to any A.x, if x is compatible with the return type
210210
def __getattr__(self, name: str) -> int: ...
211211
212+
a = A()
212213
a.foo = 42 # Works
213214
a.bar = 'Ex-parrot' # Fails type checking
214215

docs/source/command_line.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,6 @@ None and Optional handling
415415
**************************
416416

417417
The following flags adjust how mypy handles values of type ``None``.
418-
For more details, see :ref:`no_strict_optional`.
419418

420419
.. _implicit-optional:
421420

@@ -435,16 +434,19 @@ For more details, see :ref:`no_strict_optional`.
435434
436435
**Note:** This was disabled by default starting in mypy 0.980.
437436

437+
.. _no_strict_optional:
438+
438439
.. option:: --no-strict-optional
439440

440-
This flag disables strict checking of :py:data:`~typing.Optional`
441+
This flag effectively disables checking of :py:data:`~typing.Optional`
441442
types and ``None`` values. With this option, mypy doesn't
442-
generally check the use of ``None`` values -- they are valid
443-
everywhere. See :ref:`no_strict_optional` for more about this feature.
443+
generally check the use of ``None`` values -- it is treated
444+
as compatible with every type.
445+
446+
.. warning::
444447

445-
**Note:** Strict optional checking was enabled by default starting in
446-
mypy 0.600, and in previous versions it had to be explicitly enabled
447-
using ``--strict-optional`` (which is still accepted).
448+
``--no-strict-optional`` is evil. Avoid using it and definitely do
449+
not use it without understanding what it does.
448450

449451

450452
.. _configuring-warnings:

docs/source/common_issues.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ and mypy doesn't complain**.
119119
return None # No error!
120120
121121
You may have disabled strict optional checking (see
122-
:ref:`no_strict_optional` for more).
122+
:ref:`--no-strict-optional <no_strict_optional>` for more).
123123

124124
.. _silencing_checker:
125125

docs/source/config_file.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
The mypy configuration file
44
===========================
55

6+
Mypy is very configurable. This is most useful when introducing typing to
7+
an existing codebase. See :ref:`existing-code` for concrete advice for
8+
that situation.
9+
610
Mypy supports reading configuration settings from a file with the following precedence order:
711

812
1. ``./mypy.ini``
@@ -580,10 +584,15 @@ section of the command line docs.
580584
:type: boolean
581585
:default: True
582586

583-
Enables or disables strict Optional checks. If False, mypy treats ``None``
587+
Effectively disables checking of :py:data:`~typing.Optional`
588+
types and ``None`` values. With this option, mypy doesn't
589+
generally check the use of ``None`` values -- it is treated
584590
as compatible with every type.
585591

586-
**Note:** This was False by default in mypy versions earlier than 0.600.
592+
.. warning::
593+
594+
``strict_optional = false`` is evil. Avoid using it and definitely do
595+
not use it without understanding what it does.
587596

588597

589598
Configuring warnings

docs/source/error_codes.rst

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,6 @@ Most error codes are shared between multiple related error messages.
1919
Error codes may change in future mypy releases.
2020

2121

22-
23-
Displaying error codes
24-
----------------------
25-
26-
Error codes are displayed by default. Use :option:`--hide-error-codes <mypy --hide-error-codes>`
27-
or config ``hide_error_codes = True`` to hide error codes. Error codes are shown inside square brackets:
28-
29-
.. code-block:: text
30-
31-
$ mypy prog.py
32-
prog.py:1: error: "str" has no attribute "trim" [attr-defined]
33-
34-
It's also possible to require error codes for ``type: ignore`` comments.
35-
See :ref:`ignore-without-code<code-ignore-without-code>` for more information.
36-
37-
3822
.. _silence-error-codes:
3923

4024
Silencing errors based on error codes
@@ -121,3 +105,10 @@ Similar logic works for disabling error codes globally. If a given error code
121105
is a subcode of another one, it will be mentioned in the documentation for the narrower
122106
code. This hierarchy is not nested: there cannot be subcodes of other
123107
subcodes.
108+
109+
110+
Requiring error codes
111+
---------------------
112+
113+
It's possible to require error codes be specified in ``type: ignore`` comments.
114+
See :ref:`ignore-without-code<code-ignore-without-code>` for more information.

docs/source/existing_code.rst

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ invocation to your codebase, or adding your mypy invocation to
3131
existing tools you use to run tests, like ``tox``.
3232

3333
* Make sure everyone runs mypy with the same options. Checking a mypy
34-
:ref:`configuration file <config-file>` into your codebase can help
35-
with this.
34+
:ref:`configuration file <config-file>` into your codebase is the
35+
easiest way to do this.
3636

3737
* Make sure everyone type checks the same set of files. See
3838
:ref:`specifying-code-to-be-checked` for details.
@@ -48,7 +48,7 @@ A simple CI script could look something like this:
4848

4949
.. code-block:: text
5050
51-
python3 -m pip install mypy==0.971
51+
python3 -m pip install mypy==1.8
5252
# Run your standardised mypy invocation, e.g.
5353
mypy my_project
5454
# This could also look like `scripts/run_mypy.sh`, `tox run -e mypy`, `make mypy`, etc
@@ -74,6 +74,11 @@ You could even invert this, by setting ``ignore_errors = True`` in your global
7474
config section and only enabling error reporting with ``ignore_errors = False``
7575
for the set of modules you are ready to type check.
7676

77+
The per-module configuration that mypy's configuration file allows can be
78+
extremely useful. Many configuration options can be enabled or disabled
79+
only for specific modules. In particular, you can also enable or disable
80+
various error codes on a per-module basis, see :ref:`error-codes`.
81+
7782
Fixing errors related to imports
7883
--------------------------------
7984

@@ -89,7 +94,7 @@ that it can't find, that don't have types, or don't have stub files:
8994
Sometimes these can be fixed by installing the relevant packages or
9095
stub libraries in the environment you're running ``mypy`` in.
9196

92-
See :ref:`ignore-missing-imports` for a complete reference on these errors
97+
See :ref:`fix-missing-imports` for a complete reference on these errors
9398
and the ways in which you can fix them.
9499

95100
You'll likely find that you want to suppress all errors from importing
@@ -118,13 +123,15 @@ codebase, use a config like this:
118123
ignore_missing_imports = True
119124
120125
If you get a large number of errors, you may want to ignore all errors
121-
about missing imports, for instance by setting :confval:`ignore_missing_imports`
122-
to true globally. This can hide errors later on, so we recommend avoiding this
126+
about missing imports, for instance by setting
127+
:option:`--disable-error-code=import-untyped <mypy --ignore-missing-imports>`.
128+
or setting :confval:`ignore_missing_imports` to true globally.
129+
This can hide errors later on, so we recommend avoiding this
123130
if possible.
124131

125132
Finally, mypy allows fine-grained control over specific import following
126133
behaviour. It's very easy to silently shoot yourself in the foot when playing
127-
around with these, so it's mostly recommended as a last resort. For more
134+
around with these, so this should be a last resort. For more
128135
details, look :ref:`here <follow-imports>`.
129136

130137
Prioritise annotating widely imported modules

docs/source/kinds_of_types.rst

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -429,74 +429,6 @@ the runtime with some limitations (see :ref:`runtime_troubles`).
429429
430430
t2: int | None # equivalent to Optional[int]
431431
432-
.. _no_strict_optional:
433-
434-
Disabling strict optional checking
435-
**********************************
436-
437-
Mypy also has an option to treat ``None`` as a valid value for every
438-
type (in case you know Java, it's useful to think of it as similar to
439-
the Java ``null``). In this mode ``None`` is also valid for primitive
440-
types such as ``int`` and ``float``, and :py:data:`~typing.Optional` types are
441-
not required.
442-
443-
The mode is enabled through the :option:`--no-strict-optional <mypy --no-strict-optional>` command-line
444-
option. In mypy versions before 0.600 this was the default mode. You
445-
can enable this option explicitly for backward compatibility with
446-
earlier mypy versions, in case you don't want to introduce optional
447-
types to your codebase yet.
448-
449-
It will cause mypy to silently accept some buggy code, such as
450-
this example -- it's not recommended if you can avoid it:
451-
452-
.. code-block:: python
453-
454-
def inc(x: int) -> int:
455-
return x + 1
456-
457-
x = inc(None) # No error reported by mypy if strict optional mode disabled!
458-
459-
However, making code "optional clean" can take some work! You can also use
460-
:ref:`the mypy configuration file <config-file>` to migrate your code
461-
to strict optional checking one file at a time, since there exists
462-
the per-module flag
463-
:confval:`strict_optional` to control strict optional mode.
464-
465-
Often it's still useful to document whether a variable can be
466-
``None``. For example, this function accepts a ``None`` argument,
467-
but it's not obvious from its signature:
468-
469-
.. code-block:: python
470-
471-
def greeting(name: str) -> str:
472-
if name:
473-
return f'Hello, {name}'
474-
else:
475-
return 'Hello, stranger'
476-
477-
print(greeting('Python')) # Okay!
478-
print(greeting(None)) # Also okay!
479-
480-
You can still use :py:data:`Optional[t] <typing.Optional>` to document that ``None`` is a
481-
valid argument type, even if strict ``None`` checking is not
482-
enabled:
483-
484-
.. code-block:: python
485-
486-
from typing import Optional
487-
488-
def greeting(name: Optional[str]) -> str:
489-
if name:
490-
return f'Hello, {name}'
491-
else:
492-
return 'Hello, stranger'
493-
494-
Mypy treats this as semantically equivalent to the previous example
495-
if strict optional checking is disabled, since ``None`` is implicitly
496-
valid for any type, but it's much more
497-
useful for a programmer who is reading the code. This also makes
498-
it easier to migrate to strict ``None`` checking in the future.
499-
500432
.. _type-aliases:
501433

502434
Type aliases

0 commit comments

Comments
 (0)