mirror of
https://github.com/python/mypy.git
synced 2025-12-19 14:16:13 +00:00
Page:
Code Conventions
Pages
Build Manager
Code Conventions
Compiling Async Features
Compiling Generators
Compiling Nested Functions
Creating Stubs For Python Modules
Developer Guides
Exception Handling Transform
Getting Started Developing
Good Pull Request
Home
Implementation Overview
Introduction to Mypyc for Contributors
Learning Resources
Mypy Daemon
Mypyc C Code Generator
Mypyc Development Workflows
Mypyc Implementation Overview
Mypyc Intermediate Representation (IR)
Mypyc Object Representation
Mypyc Trace Logging
Profiling and Optimizing Mypy
Python Parser
Reference Counting Transform
Release Process
Semantic Analyzer
Type Checker
Typeshed
Unsupported Python Features
Using Git And GitHub
No results
8
Code Conventions
wyattscarpenter edited this page 2025-07-30 21:07:40 -04:00
Table of Contents
Python conventions
Follow PEP 8.
We use the ruff linter to enforce style rules. This greatly simplifies code reviews.
All functions must have type annotations (except for test data).
Exceptions:
- We use 99 characters as the maximum line length.
- Use spaces around = signs in default parameter values in functions with annotations:
def f(x: int = 1) -> None: ... # OK, since f is annotated
def f(x=1) -> None: ... # OK, fall back to PEP 8 if there is no annotation
Features to avoid
It's usually better to avoid these Python features (at least in performance-sensitive code):
getattr,setattr- Mypy can't type check these calls properly
- These get compiled into slow dynamic attribute gets/sets by mypyc
functools- Type checking is sometimes limited, resulting in
Anytypes - Mypyc, our compiler, often generates better code if these are replaced with more "primitive" / lower level Python code
- Type checking is sometimes limited, resulting in
copy.deepcopy- This may be dangerous with objects with complex state such as caches (we might also add such state later)
- Performance can be hard to predict
- This can cause problems in code compiled with mypyc
- Metaclasses
- Mypyc, our compiler, generates less efficient code for metaclasses
- Type checking may be limited
- Metaclasses can make code harder to understand
Anytypes- These compromise type checking precision
- Operations on
Anytypes get compiled to slow generic operations by mypyc