GuideOpen
Python - Language-Specific Review Notes
Load this file alongside rules/universal.md. Universal rules are not repeated here — only Python-specific rules and idioms.
Front matter
language: python
extensions: [".py"]Python — Language-Specific Review Notes
Load this file alongside rules/universal.md. Universal rules are not repeated here — only Python-specific rules and idioms.
PR Analyzer — Python Risk Signals
print()statements left in production code# noqaand# type: ignorecomments — verify they are justifiedeval()/exec()with any user-controlled inputpickleused to deserialize untrusted data- Hardcoded credentials or tokens in source
Code Quality — Python Checks
- Bare
except:orexcept Exception:swallowing silently - Mutable default arguments (
def foo(items=[])) — shared across calls import *— pollutes namespace and hides dependencies- Missing type hints on public functions and methods
assertused for runtime validation — stripped by-Oflag
Security
- Flag
eval()/exec()with any user-controlled input - Flag
pickle.loads()on untrusted data — usejsonormsgpack - Flag
subprocesscalls withshell=Trueand user input - Flag
flask.render_template_string()with user data (SSTI) - Flag
SECRET_KEY/DEBUG = Truecommitted to source
Async
- Flag
asyncio.get_event_loop().run_until_complete()inside an already-running loop - Flag mixing
threadingandasynciowithout a clear bridge (run_in_executor) - Flag CPU-bound work inside an
async defwithout offloading toProcessPoolExecutor - Flag
time.sleep()inside async functions — useawait asyncio.sleep()
Resource Management
- Flag
open()not used as a context manager (with open(...) as f) - Flag
requests.Sessioncreated per-request instead of shared/reused - Flag database connections not closed or returned to a pool on all paths
- Flag large files read entirely into memory with
.read()— prefer streaming / chunked reads
Exception Handling
- Flag bare
except:— catchesBaseExceptionincludingKeyboardInterruptandSystemExit - Flag
except Exception: pass— silently swallows errors - Flag re-raising with
raise einstead ofraise— loses the original traceback - Flag
exceptclause too broad when thetryblock covers multiple operations with different failure modes — split them
Performance
- Flag
+string concatenation in loops — use"".join() - Flag repeated
re.compile()inside a loop — compile once at module level - Flag
list.append()in a loop where a list comprehension would be more efficient - Flag
inmembership tests onlistwhere the collection is large — useset - Flag loading entire large files into memory — prefer streaming or chunked reads
Idioms and Best Practices
Type Safety
- All public functions and methods should have type annotations
- Prefer
X | None(Python 3.10+) overOptional[X] - Use
TypedDictordataclassover plaindictfor structured data
Modern Python (3.10+)
- Prefer
matchstatements over longif/elifchains - Prefer
dataclassorNamedTupleover plain classes for data carriers - Prefer
pathlib.Pathoveros.pathfor file operations - Prefer f-strings over
.format()or%formatting
None Safety
- Prefer explicit
if x is Noneover falsy checks when0or""are valid values - Flag functions returning
Noneimplicitly — make it explicit or raise
Author: alirezarezvani, Alireza Rezvani skills and agents. License: MIT.
Shown here with formatting for the web. Keep the author and license notice when you reuse it.
Related entries
There are more like this in the members library
The Codex library holds 7,009 entries. Members can search all of them by meaning and read every one in full.