Repository navigation
Add agent skills for migration to mypyc and mypyc optimizations - #22072
Conversation
| Class-level constants should also be annotated: | ||
|
|
||
| ``` | ||
| class Foo: | ||
| MAX_ITEMS = 100 | ||
| ``` |
There was a problem hiding this comment.
and also instance attributes that are only assigned in the constructor.
| ### Annotations Are Enforced at Runtime | ||
|
|
||
| Compiled code checks argument, return and attribute types at runtime, | ||
| and raises `TypeError` if a value has the wrong type. Code that worked | ||
| despite an inaccurate annotation may fail after compilation: |
There was a problem hiding this comment.
might be worth mentioning here to remove assertions that become unreachable because of this, for example when validating attributes passed to a function. and if those assertions are tested then the test should be changed to expect TypeError.
| * Add `@final` class decorator to internal/helper classes that conform to | ||
| these rules, if the class clearly isn't intended to be subclassed: | ||
| * They don't have any subclasses in the codebase. | ||
| * They aren't an ABC or a protocol. | ||
| * They aren't an exception class. | ||
| * They don't subclass any stdlib class (other than `object`). |
There was a problem hiding this comment.
if the class can't be made @final, annotate methods that are not overridden with @final.
| previously had none also causes mypy to type check its body; fix any new type | ||
| errors that this produces. |
There was a problem hiding this comment.
also consider using cast when a more specific type can be determined statically but cannot be used as a type annotation, for example because changing the interface of a function would cause a lot of typing issues in dependent code.
|
I'll address the comments in a follow-up PR which will likely include some other tweaks. |
Fix `check-manifest` be explicitly excluding the `skills` dir. Followup to #22072 Build failure: https://github.com/mypyc/mypy_mypyc-wheels/actions/runs/37029975663/job/110914062986#step:5:34
The idea is that these could be installed to a coding agent like Claude Code or Codex, and the agent can use the instructions to migrate/prepare code for mypyc compilation, and to optimize code for better performance under mypyc. I have two separate skills, since it often makes sense to first only do fairly minimal changes to get code to work correctly when compiled, and further optimizations can be a later step. Also, to be most effective, the optimization workflow needs some way to benchmark and profile changes, whereas the migration workflow can just check that code compiled and tests pass, so these are somewhat different use cases.
I used this with Claude Opus 5.5 to migrate a library at work to mypyc, and the results looked reasonable (though I haven't deployed the change the yet). This should be considered experimental still.