Skip to content

Commit

Permalink
Bundle misc/proper_plugin.py as a part of mypy (#16036)
Browse files Browse the repository at this point in the history
I modified 

```diff
diff --git mypy/binder.py mypy/binder.py
index 8a68f24f6..194883f86 100644
--- mypy/binder.py
+++ mypy/binder.py
@@ -345,7 +345,7 @@ class ConditionalTypeBinder:
             self._cleanse_key(dep)
 
     def most_recent_enclosing_type(self, expr: BindableExpression, type: Type) -> Type | None:
-        type = get_proper_type(type)
+        # type = get_proper_type(type)
         if isinstance(type, AnyType):
             return get_declaration(expr)
         key = literal_hash(expr)
```

to see if it still works. It is:

```python
» python runtests.py self
run self: ['/Users/sobolev/Desktop/mypy/.venv/bin/python', '-m', 'mypy', '--config-file', 'mypy_self_check.ini', '-p', 'mypy', '-p', 'mypyc']
mypy/binder.py:349: error: Never apply isinstance() to unexpanded types; use
mypy.types.get_proper_type() first  [misc]
            if isinstance(type, AnyType):
               ^~~~~~~~~~~~~~~~~~~~~~~~~
mypy/binder.py:349: note: If you pass on the original type after the check, always use its unexpanded version
Found 1 error in 1 file (checked 288 source files)

FAILED: self
```

I will add this plugin to my CI checks in like ~5 plugins I maintain :)

- https://github.com/typeddjango/django-stubs
- https://github.com/typeddjango/djangorestframework-stubs
- https://github.com/dry-python/returns
- https://github.com/dry-python/classes
- https://github.com/wemake-services/mypy-extras

Closes #16035
  • Loading branch information
sobolevn authored Sep 4, 2023
1 parent 92e054b commit 488ad4f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ include build-requirements.txt
include test-requirements.txt
include mypy_self_check.ini
prune misc
include misc/proper_plugin.py
graft test-data
include conftest.py
include runtests.py
Expand Down
9 changes: 9 additions & 0 deletions docs/source/extending_mypy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,12 @@ mypy's cache for that module so that it can be rechecked. This hook
should be used to report to mypy any relevant configuration data,
so that mypy knows to recheck the module if the configuration changes.
The hooks should return data encodable as JSON.

Useful tools
************

Mypy ships ``mypy.plugins.proper_plugin`` plugin which can be useful
for plugin authors, since it finds missing ``get_proper_type()`` calls,
which is a pretty common mistake.

It is recommended to enable it is a part of your plugin's CI.
9 changes: 9 additions & 0 deletions misc/proper_plugin.py → mypy/plugins/proper_plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
"""
This plugin is helpful for mypy development itself.
By default, it is not enabled for mypy users.
It also can be used by plugin developers as a part of their CI checks.
It finds missing ``get_proper_type()`` call, which can lead to multiple errors.
"""

from __future__ import annotations

from typing import Callable
Expand Down
2 changes: 1 addition & 1 deletion mypy_self_check.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ disallow_any_unimported = True
show_traceback = True
pretty = True
always_false = MYPYC
plugins = misc/proper_plugin.py
plugins = mypy.plugins.proper_plugin
python_version = 3.8
exclude = mypy/typeshed/|mypyc/test-data/|mypyc/lib-rt/
new_type_inference = True
Expand Down

0 comments on commit 488ad4f

Please sign in to comment.