master
Name already in use
Commits on Dec 21, 2022
-
The docs build is currently failing on `master`: see e.g. https://github.com/python/mypy/actions/runs/3748461486/jobs/6365827914. It looks like it was broken by the release of `attrs` 22.2.0 earlier today. Specifically, it looks like python-attrs/attrs@1bb2864 broke mypy's docs build. I think this fixes things.
-
Tool to compare performance of any number of mypy commits/branches (#…
…14332) The script compiles some mypy commits in parallel and then measures how long each version takes to self-check a specific mypy commit. It measures the performance 15 times for each commit and takes the average. Based on some experiments, the noise floor on my Linux desktop is about 0.5% to 1.0%. Any difference above 1.0% is likely significant, I believe. For differences between 0.5% and 1.0% it makes sense to repeat the measurement a few times. The interesting part of the output looks something like this: ``` ... === Results === 145d8a4 8.207s (0.0%) 145d8a4~1 8.105s (-1.2%) ``` Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
-
Avoid the use of a context manager in hot code path (#14331)
Mypyc can't optimize context managers yet, so it's best to avoid them in hot code paths. This sacrifices some code quality for a considerable perf gain. This improved self-check performance by 4%.
-
Refactor TypeState into a singleton class (#14327)
This helps mypyc, since accessing mutable attributes of singleton instances is faster than accessing class variables. The implementation is also arguably a bit cleaner. This seems performance-neutral or a very minor optimization, but if we continue to add attributes to TypeState, this can help.
-
Change various type queries into faster bool type queries (#14330)
I measured a 1% performance improvement in self check.
Commits on Dec 20, 2022
-
Fix RST markup in type_narrowing.rst (#14253)
Switch :py:func:type to py:class:type in type_narrowing.rst. The former does not render properly in the docs This is a tiny follow up from #14246 (comment)
-
Revert "[mypyc] Use tabs instead of spaces in emitted C code (#14016)" (
#14152) This reverts commit dbcbb3f. The indentation in generated code is now inconsistent if using 4-space tabs. We should either use tabs or spaces consistently everywhere, since we can't expect everybody to have the same tab width. The broken indentation can be seen by compiling a hello world program and opening it in an editor configured to use 4-space tabs. Since there is a lot of code in mypyc that assumes a 4-space indentation, fixing it all is probably only feasible by normalizing the indentation during the emit phase. However, the normalization step might actually slow down compilation slightly, whereas the intent of the original change to improve efficiency, so this change may ultimately be impractical. In the future we should make it possible to normalize tabs without any significant cost, but I'm not sure if that's possible right now.
-
Allow 'in' to narrow TypedDict unions (#13838)
`in` could narrow unions of TypeDicts, e.g. ```python class A(TypedDict) foo: int @Final class B(TypedDict): bar: int union: Union[A, B] = ... value: int if 'foo' in union: # Cannot be a B as it is final and has no "foo" field, so must be an A value = union['foo'] else: # Cannot be an A as those went to the if branch value = union['bar'] ```
-
Speed up recursive type check (#14326)
Use a faster type query visitor and reuse visitor across calls. This should speed up type checking slightly. My measurements show a ~0.5% improvement, but it may be below the noise floor.
-
Optimize subtype checking by avoiding a nested function (#14325)
Mypyc isn't good at compiling nested functions, and this one was in one of the hottest code paths in all of mypy. The nested function wasn't even used that often, but mypyc would still construct a closure object every time. This adds some code duplication, but it's well worth it. Amazingly, this speeds up self-check by about 10%, if my measurements are to be trusted! This addresses some of the slowdown introduced in #13303. #14324 addresses another related slowdown.
-
Optimize type parameter checks in subtype checking (#14324)
Avoid the use of a nested function, which are a bit slow when compiled with mypyc. Also avoid a callable value and instead call a function directly, which allows using faster native calls. Based on a quick experiment, this speeds up self check by about 3%. This addresses some of the slowdown introduced in #13303.
-
Speed up freshening type variables (#14323)
Only perform type variable freshening if it's needed, i.e. there is a nested generic callable, since it's fairly expensive. Make the check for generic callables fast by creating a specialized type query visitor base class for queries with bool results. The visitor tries hard to avoid memory allocation in typical cases, since allocation is slow. This addresses at least some of the performance regression in #14095. This improved self-check performance by about 3% when compiled with mypyc (-O2). The new visitor class can potentially help with other type queries as well. I'll explore it in follow-up PRs.
-
Optimize implementation of TypedDict types for **kwds (#14316)
The implementation copied lots of callable types even when not using the new feature, which was expensive. Now we only generate a copy if a callable actually uses TypedDict types for **kwds. This made self check 7-8% faster (when compiled with -O0). The original implementation was in #13471.
-
Update
stubinfo.pyfor recent typeshed changes (#14265)Removals from `stubinfo.py`: - `atomicwrites` is archived and deprecated at runtime; stubs were removed from typeshed in python/typeshed#8925 - `attrs` has had inline types for a very long time now - `chardet` recently cut a release with inline types; typeshed's stubs were marked obsolete in python/typeshed#9318 - `cryptography` has had inline types for a very long time now; the only reason why it's still in typeshed is because other typeshed packages need `types-cryptography` as a dependency, and our testing infrastructure therefore can't currently cope with it being removed from typeshed. - `emoji` recently cut a release bundling stubs with the runtime package; typeshed's stubs were marked obsolete in python/typeshed#9051 - `termcolor` recently cut a release with inline types; typeshed's stubs were marked obsolete in python/typeshed#8746 - `prettytable` recently cut a release with inline types; typeshed's stubs were marked obsolete in python/typeshed#9023 Additions: - Stubs for `Xlib` were added in python/typeshed#9279 - Stubs for `consolemenu` were added in python/typeshed#8820 - Stubs for `dockerfile_parse` were added in python/typeshed#9305 - Stubs for `flask_migrate` were added in python/typeshed#8967 - Stubs for `paho.mqtt` were added in python/typeshed#8853 - Stubs for `pycocotools` were added in python/typeshed#9086 - Stubs for many `pywin32` modules were added in python/typeshed#8825, and multiple follow-up PRs - Stubs for `pyscreeze` were added in python/typeshed#8823
Commits on Dec 19, 2022
-
Enable
--debug-serializefor mypy_primer (#14318)Enable the `--debug-serialize` option to help catch issues during serialization which would normally be skipped by mypy_primer. Followup to #14155
-
Add
--debug-serializeoption (#14155)Currently, `mypy_primer` sets `--cache-dir=/dev/null` which disables cache generation. This can result in errors being missed which would normally come up during `tree.serialize()`. Removing `--cache-dir=/dev/null` isn't practical. This PR adds a new debug / test option `--debug-serialize` which runs `tree.serialize()` even if cache generation is disabled to help detect serialize errors earlier. **Refs** * #14137 * hauntsaninja/mypy_primer#54 (review) cc: @hauntsaninja
-
Add basic support for
typing_extensions.TypeVar(#14313)This PR only adds the existing `TypeVar` support for the `typing_extensions` variant. I.e. it does not include support for `default` or `infer_variance`. Fixes #14312
-
[undefined vars] skip visiting unreachable else clauses (#14308)
In particular, ran into an issue with an `if TYPE_CHECKING` case, so I added a test for that.
-
[used-before-def] fix false positive inside loop (#14307)
A similar case was addressed in #14176 but I missed the part where it doesn't need to be defined in a different branch. This makes the fix more complete.
Commits on Dec 16, 2022
-
[partially defined] implement support for try statements (#14114)
This adds support for try/except/finally/else check. The implementation ended up pretty complicated because it had to handle jumps different for finally. It took me a few iterations to get to this solution and that's the cleanest one I could come up with. Closes #13928.
Commits on Dec 15, 2022
-
Constant fold initializers of final variables (#14283)
Now mypy can figure out the values of final variables even if the initializer has some operations on constant values: ``` A: Final = 2 # This has always worked A: Final = -(1 << 2) # This is now supported B: Final = 'x' + 'y' # This also now works ``` Currently we support integer arithmetic and bitwise operations, and string concatenation. This can be useful with literal types, but my main goal was to improve constant folding in mypyc. In particular, this helps constant folding with native ints in cases like these: ``` FLAG1: Final = 1 << 4 FLAG2: Final = 1 << 5 def f() -> i64: return FLAG1 | FLAG2 # Can now be constant folded ``` We still have another constant folding pass in mypyc, since it does some things more aggressively (e.g. it constant folds some member expression references). Work on mypyc/mypyc#772. Also helps with mypyc/mypyc#862. -
Source commit: python/typeshed@9bddd3a
Commits on Dec 14, 2022
Commits on Dec 12, 2022
-
Replace unsafe PY_*_VERSION comparisons to fix for Python 4.0 (#14280)
`#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 7` For example, this is true for Python 3.7-3.11 but also true for Python 4.7-4.11. Instead, `PY_VERSION_HEX` should be used. https://docs.python.org/3.11/c-api/apiabiversion.html ```c /* Version as a single 4-byte hex number, e.g. 0x010502B2 == 1.5.2b2. Use this for numeric comparisons, e.g. #if PY_VERSION_HEX >= ... */ ``` https://github.com/python/cpython/blob/2e279e85fece187b6058718ac7e82d1692461e26/Include/patchlevel.h#L29-L30 Remove compatibility code that only applied to EOL and unsupported Python versions (<= 3.6)
Commits on Dec 10, 2022
-
Rename
partially-definederror codes (#14267)Rename `partially-defined` to `possibly-undefined` and `use-before-def` to `used-before-def`. Ref #14226
Commits on Dec 7, 2022
-
Last time i checked it wasn't the 1960's. So I think the CI could be colorized. Configured pytest, tox, mypy(#7771) and pip¹ (I already already colorized black and isort when I initially added them) 1: Pip doesn't work yet pypa/pip#10909, so this is just a placedholder for when it (hopefully) soon will. Co-authored-by: KotlinIsland <kotlinisland@users.noreply.github.com>
Commits on Dec 6, 2022
-
Replace TypeList in constraints with TupleType (#14257)
Now that the fallback is available, we can construct TupleTypes instead of TypeLists which will simplify constraint solving as it won't need to know to match TupleTypes with TypeLists.
-
[mypyc] Support tuples of native ints (#14252)
A tuple such as `tuple[i64, i64]` can't have a dedicated error value, so use overlapping error values, similarly to how we support plain native integers such as `i64`. For a heterogeneous tuple such as as `tuple[i64, str]` we attempt to store the error value in the smallest item index where the value type supports error values (1 in this example). This affects error returns, undefined attributes, default arguments and undefined locals.
-
Enable Final instance attributes for attrs (#14232)
A quick patch to enable the following scenario: ```python @define class C: a: Final[int] # `a` is a final instance attribute ``` There are some edge cases I haven't covered here that would be complex to handle and not add much value IMO, so I think this will be useful like this.
-
Allow trailing commas in
iniconfiguration of multiline values (#14240) Now these two samples are identical: ```ini [mypy] enable_error_code = truthy-bool, redundant-expr, unused-awaitable, ignore-without-code ``` and ```ini [mypy] enable_error_code = truthy-bool, redundant-expr, unused-awaitable, ignore-without-code, ``` I've covered some of the changed values, but not all (they are identical - no need to create so many slow tests). I've also checked `pyproject.toml`. It does not have this problem: it uses `[]` to create arrays, so no trailing commas are used there.
Commits on Dec 5, 2022
-
Advertise mypy daemon in README (#14248)
Now that daemon is more mature and stable I think we can attract some more attention to it.
-
Add link to error codes in README (#14249)
Several issues that I have seen recently could be fixed by enabling/disabling an error code. I think we should attract more attention to error code customization (especially for new users).
Commits on Dec 4, 2022
-
Stop saying mypy is beta software (#14251)
Ref #13685 cc @Michael0x2a @JukkaL @hauntsaninja Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>