Skip to content

The Final patch #7

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

Open
wants to merge 120 commits into
base: base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
120 commits
Select commit Hold shift + click to select a range
93e6bfc
Migrate checkstrformat to use `ErrorMessage` class
tushar-deepsource Jan 20, 2022
dcf0942
Migrate messages.py to ErrorMessage class
tushar-deepsource Jan 20, 2022
a29318a
Typo fix
tushar-deepsource Jan 20, 2022
9b2c728
Lint
tushar-deepsource Jan 20, 2022
0a05cae
Fix message
tushar-deepsource Jan 20, 2022
58517d4
Update error codes types
tushar-deepsource Jan 20, 2022
792331f
Fix it again
tushar-deepsource Jan 20, 2022
717e176
Undo unintended changes
tushar-deepsource Jan 20, 2022
a63cba8
Fix fail signature
tushar-deepsource Jan 20, 2022
52b97bc
Bugfixes
tushar-deepsource Jan 20, 2022
30dee54
Fix message text
tushar-deepsource Jan 20, 2022
fa8f099
Fix old version too
tushar-deepsource Jan 20, 2022
0cec4f7
Microrefactoring: removed useless var from `meet.py` (#12025)
sobolevn Jan 20, 2022
01f0bc8
Remove line
tushar-deepsource Jan 20, 2022
04d3490
Migrate plugins to use ErrorMessage class
tushar-deepsource Jan 20, 2022
979f8c3
Fix remaining issues
tushar-deepsource Jan 20, 2022
fc6d5be
Remove test data error message
tushar-deepsource Jan 20, 2022
5cc51a7
Merge branch 'messages-messages' into final
tushar-deepsource Jan 20, 2022
826cc86
Merge branch 'plugins-messages' into final
tushar-deepsource Jan 20, 2022
e178323
The final patch to wrap everything up
tushar-deepsource Jan 20, 2022
6d24332
stubtest: treat dicts as a subtype of typeddict (#12040)
hauntsaninja Jan 23, 2022
619d9bd
Fix crash with yield in comprehension (#12048)
Jan 24, 2022
af366c0
Explain generic `Protocol[T1, T2, ...]` shorthand in Mypy docs (#12047)
posita Jan 24, 2022
3680449
Simplify unions when erasing last known values (#12064)
JukkaL Jan 25, 2022
fb94f80
Fix join of Any against a union type (#12068)
JukkaL Jan 25, 2022
99f4d5a
Add note about wrong error code in type: ignore (#12067)
JukkaL Jan 25, 2022
d5d077b
Fix crash involving explicit any flag and Required (#12039)
hmc-cs-mdrissi Jan 26, 2022
a825c15
Fix error in `exclude` section (#12078)
posita Jan 27, 2022
e285203
[docs] add more info to allow_redefinition (#11951)
KotlinIsland Jan 27, 2022
b36d8cf
stubtest: use VERSIONS for submodules (#12083)
hauntsaninja Jan 27, 2022
e266cdf
Sync typeshed (#11905)
hauntsaninja Jan 28, 2022
7af46ce
Forbid extra `ParamSpec` arguments (#12024)
sobolevn Jan 28, 2022
ace9f25
Delete workarounds for fixed typeshed bugs (#12093)
Akuli Jan 28, 2022
8f9ebf0
Run self-check on Windows as part of CI (#11909)
AlexWaygood Jan 28, 2022
080bb0e
stubtest: error if a class should be decorated with @final (#12091)
Akuli Jan 30, 2022
3b331b3
Fix "Implicit type aliases not recognised with PEP 604 + import cycle…
AlexWaygood Jan 30, 2022
c0e49ab
Read pyproject.toml with correct encoding on Windows (#12105)
domdfcoding Feb 1, 2022
84b9778
add support for typing.reveal_type (#12117)
JelleZijlstra Feb 3, 2022
04cac4b
[mypyc] Add helpful message to assert (#12119)
thejcannon Feb 4, 2022
26fdd45
Update mypy_primer_comment.yml
sobolevn Feb 5, 2022
48dc990
Sync `mypy_primer_comment` with `typeshed` (#12125)
sobolevn Feb 6, 2022
1ccefbd
Improve `.editorconfig` to include `.test` files (#12133)
sobolevn Feb 6, 2022
872c03d
Sync `mypy_primer_comment` with `typeshed` (#12136)
sobolevn Feb 7, 2022
0647570
Delete `open` plugin (#9275)
hauntsaninja Feb 9, 2022
37d58c3
Add `StrEnum` support for Python 3.11 (#12035)
sobolevn Feb 9, 2022
234344b
Mention common resolutions for build errors (#12154)
hauntsaninja Feb 9, 2022
6ff8091
Add support for Never (#12153)
JelleZijlstra Feb 9, 2022
84696ce
Add an option to require ignore comments have error codes (#11633)
PeterJCLaw Feb 9, 2022
129dba4
Update CREDITS (#12018)
97littleleaf11 Feb 10, 2022
5b9318d
Add `match_args` support to `attr.s()` (#12111)
sobolevn Feb 12, 2022
595fec9
Fix CI (#12181)
hauntsaninja Feb 15, 2022
cd3dfbd
Fix use of TypeAlias from aliased imports (#12180)
hauntsaninja Feb 15, 2022
777885f
stubtest: enable checking pos-only differences in dunders (#12184)
AlexWaygood Feb 15, 2022
554606b
Suggest `typing.Any` when using `any` as type (#12185)
siiptuo Feb 16, 2022
eef316d
Understand the self-destructing nature of Enum._ignore_ (#12128)
kstauffer Feb 16, 2022
84aaef5
CONTRIBUTING.md: more words for first-time contributors (#12193)
hauntsaninja Feb 16, 2022
1de5e55
Fix crashes in class scoped imports (#12023)
hauntsaninja Feb 17, 2022
5b4ffa9
Deduplicate error codes for `ignore-without-code` (#12194)
cdce8p Feb 17, 2022
3158677
Fix more crashes in class scoped imports (#12199)
hauntsaninja Feb 17, 2022
d02db50
Suggest `typing.Callable` when using `callable` as type (#12204)
siiptuo Feb 17, 2022
c7365ef
[mypyc] Implement additional ircheck checks (#12191)
jhance Feb 18, 2022
85fc99c
stubtest: error if a function is async at runtime but not in the stub…
AlexWaygood Feb 19, 2022
36d50d6
Tweak ignore-without-code error message (#12216)
cdce8p Feb 19, 2022
9af578d
stubtest: error if a dunder method is missing from a stub (#12203)
AlexWaygood Feb 19, 2022
f98881c
stubtest: ignore __main__ module (#12218)
hauntsaninja Feb 20, 2022
58514a9
stubtest: error if module level dunder is missing, housekeeping (#12217)
hauntsaninja Feb 20, 2022
608f274
stubtest: catch more getattr errors (#12219)
hauntsaninja Feb 20, 2022
a726892
[mypyc] Refactor: Use WrapperGenerator for generate_wrapper_core (#12…
97littleleaf11 Feb 20, 2022
68b208d
Coalesce Literals when printing Unions (#12205)
intgr Feb 20, 2022
6698263
Update dev-intro.md
97littleleaf11 Feb 20, 2022
b22c4e4
BUG: Fix an `issubclass` failure for protocols with overloaded method…
BvB93 Feb 20, 2022
2d30dba
Fix one invalid reference inside visit_class_pattern (#12229)
97littleleaf11 Feb 21, 2022
2c9a8e7
Sync pythoncapi_compat (#12188)
97littleleaf11 Feb 22, 2022
a8b6d6f
Fix inference of protocol against overloaded function (#12227)
JukkaL Feb 22, 2022
1321e9e
Check `Enum` definition for invalid base classes (#12026)
sobolevn Feb 22, 2022
8157a6f
Typeshed cherry-pick: Improve signature of overlaps in ipaddress (#72…
JukkaL Feb 22, 2022
7143424
Handle `raise Exception(), None` on Python2.7 (#11786)
sobolevn Feb 22, 2022
bd37ab8
Adds docs about `Enum` type (#11805)
sobolevn Feb 22, 2022
784b67e
[mypyc] Use Py_TYPE and Py_IsNone (#12233)
97littleleaf11 Feb 22, 2022
bb1fda3
Typeshed cherry-pick: Replace some literal types in logging with int …
JukkaL Feb 22, 2022
eca4c30
Typeshed cherry-pick: Use `int | Any` for `types.FrameType.f_lineno` …
JukkaL Feb 22, 2022
65ac270
Use type variable bound to infer constraints (#12230)
JukkaL Feb 22, 2022
fa3adbd
Update `Enum` docs (#12238)
sobolevn Feb 23, 2022
a3fc35a
Make pyinfo compatible with Python 2 (#12244)
hauntsaninja Feb 23, 2022
115ac31
[mypyc] Improve comments and docstrings in tests (#12223)
JukkaL Feb 23, 2022
14de8c6
Use `__truediv__` for python2 with `__future__` import, refs #11740 (…
sobolevn Feb 23, 2022
ea93326
Give "as" variables in with statements separate scopes (#12254)
JukkaL Feb 25, 2022
feca706
renaming: minor comment tweaks (#12255)
JukkaL Feb 25, 2022
e784648
stubtest: do not error if a stub is async, but runtime is not (#12234)
AlexWaygood Feb 27, 2022
feab209
Fix enum regression #12258 (#12260)
sobolevn Feb 28, 2022
187f394
Remove orjson stubs from default list (#12264)
hauntsaninja Mar 1, 2022
26bfc63
Improve the "Argument must be a mapping" error message (#12222)
purna135 Mar 2, 2022
7b4d725
Remove test case for TypedDict with kwargs (#12273)
97littleleaf11 Mar 2, 2022
68b3b27
Support universal2 (#10651)
97littleleaf11 Mar 2, 2022
0777c10
Add support for conditionally defined overloads (#10712)
cdce8p Mar 3, 2022
73a5b3c
Small doc improvements for conditional overloads (#12283)
cdce8p Mar 3, 2022
a562f0a
stubtest: Fix wrong assumption about relative path (#12282)
stanislavlevin Mar 4, 2022
4d99f7c
teststubtest: further fix tests (#12287)
hauntsaninja Mar 4, 2022
b3752a6
Revert "stubtest pos-only differences in dunders (#12184)" (#12293)
hauntsaninja Mar 5, 2022
c7a8162
stubtest: ignore more dunder positional-only errors (#12294)
hauntsaninja Mar 5, 2022
fce1b54
CI: Do not run mypy_primer on stubtest/stubgen PRs (#12295)
AlexWaygood Mar 5, 2022
226661f
Exhaustiveness checking for match statements (#12267)
JukkaL Mar 7, 2022
8650f5c
stubgen: fix non default keyword-only argument positioning (#12303)
shoracek Mar 8, 2022
5954bc9
Update copyright in LICENSE (#12330)
JukkaL Mar 10, 2022
942395a
Minor documentation updates (#12329)
JukkaL Mar 10, 2022
7390280
Trigger wheel builds
JukkaL Mar 11, 2022
48ff811
Bump version to 0.940
JukkaL Mar 11, 2022
8c11393
Typeshed cherry-pick: Use import instead of type alias in `email/mess…
offbyone Mar 14, 2022
4e18c78
Bump version to 0.941
JukkaL Mar 14, 2022
8e9ac15
Bump version to 0.942+dev
JukkaL Mar 21, 2022
626147a
Fix small conditional overload regression (#12336)
cdce8p Mar 12, 2022
6606dbe
Allow non-final __match_args__ and overriding (#12415)
JukkaL Mar 22, 2022
837543e
Fix crash in match statement if class name is undefined (#12417)
JukkaL Mar 22, 2022
7e09c2a
Support overriding dunder attributes in Enum subclass (#12138)
Mar 22, 2022
f81b228
Fix inheritance false positives with dataclasses/attrs (#12411)
JukkaL Mar 22, 2022
367b29d
Make order of processing the builtins SCC predictable (#12431)
JukkaL Mar 23, 2022
67088e5
Pin the version of bugbear (#12436)
jhance Mar 23, 2022
1c83668
Let overload item have a wider return type than implementation (#12435)
JukkaL Mar 24, 2022
38f1e30
Update version to 0.942
JukkaL Mar 24, 2022
55a9076
Merge tag 'v0.942' into final
tushar-deepsource Apr 11, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
root = true

[*.{py,c,cpp,h,rst,md,yml,json}]
[*.{py,c,cpp,h,rst,md,yml,json,test}]
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space

[*.{py,c,h,json}]
[*.{py,c,h,json,test}]
indent_size = 4

[*.yml]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
with:
python-version: '3.7'
- name: Install tox
run: pip install --upgrade 'setuptools!=50' 'virtualenv<16.7.11' tox==3.20.1
run: pip install --upgrade 'setuptools!=50' 'virtualenv>=20.6.0' tox==3.24.5
- name: Setup tox environment
run: tox -e ${{ env.TOXENV }} --notest
- name: Test
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/mypy_primer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ on:
- '**/*.rst'
- '**/*.md'
- 'mypyc/**'
- 'mypy/stubtest.py'
- 'mypy/stubgen.py'
- 'mypy/stubgenc.py'
- 'mypy/test/**'

jobs:
mypy_primer:
Expand Down
73 changes: 26 additions & 47 deletions .github/workflows/mypy_primer_comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,62 +38,41 @@ jobs:
fs.writeFileSync("diff.zip", Buffer.from(download.data));

- run: unzip diff.zip

# Based on https://github.com/kanga333/comment-hider
- name: Hide old comments
uses: actions/github-script@v3
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const fs = require('fs')

const response = await github.issues.listComments({
issue_number: fs.readFileSync("pr_number.txt", { encoding: "utf8" }),
owner: context.repo.owner,
repo: context.repo.repo,
})
const botCommentIds = response.data
.filter(comment => comment.user.login === 'github-actions[bot]')
.map(comment => comment.node_id)

for (const id of botCommentIds) {
const resp = await github.graphql(`
mutation {
minimizeComment(input: {classifier: OUTDATED, subjectId: "${id}"}) {
minimizedComment {
isMinimized
}
}
}
`)
if (resp.errors) {
throw new Error(resp.errors)
}
}
# 30000 bytes is about 300 lines, posting comment fails if too long
- run: |
cat diff_*.txt | head -c 30000 | tee fulldiff.txt

- name: Post comment
id: post-comment
uses: actions/github-script@v3
with:
github-token: ${{secrets.GITHUB_TOKEN}}
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs')
// Keep in sync with shards produced by mypy_primer workflow
const data = (
['diff_0.txt', 'diff_1.txt', 'diff_2.txt']
.map(fileName => fs.readFileSync(fileName, { encoding: 'utf8' }))
.join('')
.substr(0, 30000) // About 300 lines
)
const data = fs.readFileSync('fulldiff.txt', { encoding: 'utf8' })

console.log("Diff from mypy_primer:")
console.log(data)

let body
if (data.trim()) {
const body = 'Diff from [mypy_primer](https://github.com/hauntsaninja/mypy_primer), showing the effect of this PR on open source code:\n```diff\n' + data + '```'
await github.issues.createComment({
issue_number: fs.readFileSync("pr_number.txt", { encoding: "utf8" }),
owner: context.repo.owner,
repo: context.repo.repo,
body
})
body = 'Diff from [mypy_primer](https://github.com/hauntsaninja/mypy_primer), showing the effect of this PR on open source code:\n```diff\n' + data + '```'
} else {
body = 'According to [mypy_primer](https://github.com/hauntsaninja/mypy_primer), this change has no effect on the checked open source code. 🤖🎉'
}
const prNumber = parseInt(fs.readFileSync("pr_number.txt", { encoding: "utf8" }))
await github.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body
})
return prNumber

- name: Hide old comments
# v0.3.0
uses: kanga333/comment-hider@bbdf5b562fbec24e6f60572d8f712017428b92e0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
leave_visible: 1
issue_number: ${{ steps.post-comment.outputs.result }}
9 changes: 7 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,16 @@ jobs:
toxenv: py36
tox_extra_args: "-n 2 mypyc/test/test_run.py mypyc/test/test_external.py"
debug_build: true
- name: Type check our own code
- name: Type check our own code (py37-ubuntu)
python: '3.7'
arch: x64
os: ubuntu-latest
toxenv: type
- name: Type check our own code (py37-windows-64)
python: '3.7'
arch: x64
os: windows-latest
toxenv: type
- name: Code style with flake8
python: '3.7'
arch: x64
Expand All @@ -102,7 +107,7 @@ jobs:
./misc/build-debug-python.sh $PYTHONVERSION $PYTHONDIR $VENV
source $VENV/bin/activate
- name: Install tox
run: pip install --upgrade 'setuptools!=50' 'virtualenv>=20.6.0' tox==3.20.1
run: pip install --upgrade 'setuptools!=50' 'virtualenv>=20.6.0' tox==3.24.5
- name: Compiled with mypyc
if: ${{ matrix.test_mypyc }}
run: |
Expand Down
25 changes: 17 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ You can also use `tox` to run tests, for instance:
tox -e py
```

The easiest way to run a single test is:
```
pytest -n0 -k 'test_name'
```
There's more useful information on writing and running tests [here](test-data/unit/README.md)

## First time contributors

If you're looking for things to help with, browse our [issue tracker](https://github.com/python/mypy/issues)!
Expand All @@ -63,21 +69,24 @@ In particular, look for:
- [good second issues](https://github.com/python/mypy/labels/good-second-issue)
- [documentation issues](https://github.com/python/mypy/labels/documentation)

It's also extremely easy to get started contributing to our sister project
[typeshed](https://github.com/python/typeshed/issues) that provides type stubs
for libraries. This is a great way to become familiar with type syntax.

If you need help getting started, don't hesitate to ask on
[gitter](https://gitter.im/python/typing).
You do not need to ask for permission to work on any of these issues.
Just fix the issue yourself, [try to add a unit test](#running-tests) and
[open a pull request](#submitting-changes).

To get help fixing a specific issue, it's often best to comment on the issue
itself. The more details you provide about what you've tried and where you've
looked, the easier it will be for you to get help.
itself. You're much more likely to get help if you provide details about what
you've tried and where you've looked (maintainers tend to help those who help
themselves). [gitter](https://gitter.im/python/typing) can also be a good place
to ask for help.

Interactive debuggers like `pdb` and `ipdb` are really useful for getting
started with the mypy codebase. This is a
[useful tutorial](https://realpython.com/python-debugging-pdb/).

It's also extremely easy to get started contributing to our sister project
[typeshed](https://github.com/python/typeshed/issues) that provides type stubs
for libraries. This is a great way to become familiar with type syntax.

## Submitting changes

Even more excellent than a good bug report is a fix for a bug, or the
Expand Down
7 changes: 5 additions & 2 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ Dropbox core team:

Jukka Lehtosalo <[email protected]>
Ivan Levkivskyi <[email protected]>
Jared Hance

Non-Dropbox core team members:

Ethan Smith
Guido van Rossum <[email protected]>
Jelle Zijlstra
Jelle Zijlstra <[email protected]>
Michael J. Sullivan <[email protected]>
Shantanu Jain
Xuanda Yang
Xuanda Yang <[email protected]>
Jingchen Ye <[email protected]>
Nikita Sobolev <[email protected]>

Past Dropbox core team members:

Expand Down
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Mypy (and mypyc) are licensed under the terms of the MIT license, reproduced bel

The MIT License

Copyright (c) 2015-2021 Jukka Lehtosalo and contributors
Copyright (c) 2012-2022 Jukka Lehtosalo and contributors
Copyright (c) 2015-2022 Dropbox, Inc.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
Expand Down
11 changes: 10 additions & 1 deletion docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,16 @@ of the above sections.
# 'items' has type list[str]
items = [item.split() for item in items]
# 'items' now has type list[list[str]]
...

The variable must be used before it can be redefined:

.. code-block:: python

def process(items: list[str]) -> None:
items = "mypy" # invalid redefinition to str because the variable hasn't been used yet
print(items)
items = "100" # valid, items now has type str
items = int(items) # valid, items now has type int

.. option:: --local-partial-types

Expand Down
32 changes: 26 additions & 6 deletions docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ section of the command line docs.

A regular expression that matches file names, directory names and paths
which mypy should ignore while recursively discovering files to check.
Use forward slashes on all platforms.
Use forward slashes (``/``) as directory separators on all platforms.

.. code-block:: ini

Expand Down Expand Up @@ -237,8 +237,9 @@ section of the command line docs.

[tool.mypy]
exclude = [
"^file1\\.py$", # TOML's double-quoted strings require escaping backslashes
'^file2\.py$', # but TOML's single-quoted strings do not
"^one\\.py$", # TOML's double-quoted strings require escaping backslashes
'two\.pyi$', # but TOML's single-quoted strings do not
'^three\.',
]

A single, multi-line string:
Expand All @@ -247,9 +248,10 @@ section of the command line docs.

[tool.mypy]
exclude = '''(?x)(
^file1\.py$
|^file2\.py$,
)'''
^one\.py$ # files named "one.py"
| two\.pyi$ # or files ending with "two.pyi"
| ^three\. # or files starting with "three."
)''' # TOML's single-quoted strings do not require escaping backslashes

See :ref:`using-a-pyproject-toml`.

Expand Down Expand Up @@ -613,6 +615,24 @@ section of the command line docs.

Allows variables to be redefined with an arbitrary type, as long as the redefinition
is in the same block and nesting level as the original definition.
Example where this can be useful:

.. code-block:: python

def process(items: list[str]) -> None:
# 'items' has type list[str]
items = [item.split() for item in items]
# 'items' now has type list[list[str]]

The variable must be used before it can be redefined:

.. code-block:: python

def process(items: list[str]) -> None:
items = "mypy" # invalid redefinition to str because the variable hasn't been used yet
print(items)
items = "100" # valid, items now has type str
items = int(items) # valid, items now has type int

.. confval:: local_partial_types

Expand Down
33 changes: 33 additions & 0 deletions docs/source/error_code_list2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,36 @@ no error in practice. In such case, it might be prudent to annotate ``items: Seq
This is similar in concept to ensuring that an expression's type implements an expected interface (e.g. ``Sized``),
except that attempting to invoke an undefined method (e.g. ``__len__``) results in an error,
while attempting to evaluate an object in boolean context without a concrete implementation results in a truthy value.


.. _ignore-without-code:

Check that ``# type: ignore`` include an error code [ignore-without-code]
-------------------------------------------------------------------------

Warn when a ``# type: ignore`` comment does not specify any error codes.
This clarifies the intent of the ignore and ensures that only the
expected errors are silenced.

Example:

.. code-block:: python

# mypy: enable-error-code ignore-without-code

class Foo:
def __init__(self, name: str) -> None:
self.name = name

f = Foo('foo')

# This line has a typo that mypy can't help with as both:
# - the expected error 'assignment', and
# - the unexpected error 'attr-defined'
# are silenced.
# Error: "type: ignore" comment without error code (consider "type: ignore[attr-defined]" instead)
f.nme = 42 # type: ignore

# This line warns correctly about the typo in the attribute name
# Error: "Foo" has no attribute "nme"; maybe "name"?
f.nme = 42 # type: ignore[assignment]
4 changes: 4 additions & 0 deletions docs/source/error_codes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ or config `show_error_codes = True` to display error codes. Error codes are show
$ mypy --show-error-codes prog.py
prog.py:1: error: "str" has no attribute "trim" [attr-defined]

It's also possible to require error codes for ``type: ignore`` comments.
See :ref:`ignore-without-code<ignore-without-code>` for more information.


.. _silence-error-codes:

Silencing errors based on error codes
Expand Down
4 changes: 4 additions & 0 deletions docs/source/generics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,10 @@ protocols mostly follow the normal rules for generic classes. Example:
y: Box[int] = ...
x = y # Error -- Box is invariant

Per :pep:`PEP 544: Generic protocols <544#generic-protocols>`, ``class
ClassName(Protocol[T])`` is allowed as a shorthand for ``class
ClassName(Protocol, Generic[T])``.

The main difference between generic protocols and ordinary generic
classes is that mypy checks that the declared variances of generic
type variables in a protocol match how they are used in the protocol
Expand Down
Loading