fix: replace mutable default arg and add encoding to open() calls#15493
fix: replace mutable default arg and add encoding to open() calls#15493KeloYuan wants to merge 1 commit into
Conversation
…o open() calls - fastapi/_compat/v2.py: Replace mutable default argument with in ModelField.validate() to avoid shared state across calls (B006/W0102) - scripts/add_latest_release_date.py: Add encoding='utf-8' to both open() calls to ensure consistent file reading/writing across platforms (W1514)
There was a problem hiding this comment.
Pull request overview
This PR addresses two cross-platform Python pitfalls in FastAPI’s codebase: avoiding mutable default arguments in a compatibility shim, and making a docs-maintenance script’s file I/O deterministic across locales by specifying UTF-8.
Changes:
- Replaced a mutable default argument (
{}) withNoneinModelField.validate()in the Pydantic v2 compat layer. - Added
encoding="utf-8"to the read and writeopen()calls inscripts/add_latest_release_date.py.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
fastapi/_compat/v2.py |
Removes a mutable default arg from ModelField.validate() to avoid shared-state bugs and satisfy bugbear/ruff rules. |
scripts/add_latest_release_date.py |
Makes script file reads/writes consistent across platforms by explicitly using UTF-8 encoding. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@KeloYuan, thanks! Let's only fix second issue (with encoding). "Mutable default argument" is not an issue - this parameter is not used. This was probably done this way for for compatibility with Pydantic V1. It will likely be rewritten at some point. Could you please update PR and its description to exclude the part for "Mutable default argument"? |
Summary
This PR fixes two categories of code quality issues:
1. Mutable default argument in
fastapi/_compat/v2.pyFile:
fastapi/_compat/v2.pyReplaced the mutable default argument
values: dict[str, Any] = {}inModelField.validate()withvalues: dict[str, Any] | None = None. Using a mutable object (like{}) as a default argument is a well-known Python pitfall (Pylint W0102 / flake8 B006) — the same dict instance is shared across all calls, which can lead to subtle bugs if the dict is ever mutated.2.
open()calls without explicit encoding inscripts/File:
scripts/add_latest_release_date.pyAdded
encoding='utf-8'to bothopen()calls (read and write). Without an explicit encoding, Python falls back to the platform locale default, which can differ between developer machines and CI environments (e.g. Windows uses cp1252 by default). Explicitly specifying UTF-8 ensures consistent behaviour everywhere (Pylint W1514).Changes
fastapi/_compat/v2.py:values: dict[str, Any] = {}→values: dict[str, Any] | None = Nonescripts/add_latest_release_date.py:open(RELEASE_NOTES_FILE)→open(RELEASE_NOTES_FILE, encoding='utf-8')scripts/add_latest_release_date.py:open(RELEASE_NOTES_FILE, 'w')→open(RELEASE_NOTES_FILE, 'w', encoding='utf-8')