Skip to content

Validate init aliases before generated code compilation#1556

Open
uwezkhan wants to merge 3 commits into
python-attrs:mainfrom
uwezkhan:fix/init-alias-validation
Open

Validate init aliases before generated code compilation#1556
uwezkhan wants to merge 3 commits into
python-attrs:mainfrom
uwezkhan:fix/init-alias-validation

Conversation

@uwezkhan
Copy link
Copy Markdown

attrs generates init methods dynamically using field aliases. currently invalid aliases can make it all the way into generated source code and fail later during compilation with confusing SyntaxError exceptions.

this change adds validation for init aliases before code generation happens. it checks for invalid identifiers, python keywords, reserved names like self and unicode normalization collisions that can result in duplicate generated parameter names.

validation happens after alias resolution and field transformers run so all final aliases are checked consistently before generating the initializer.

added regression tests for:

invalid identifiers
keyword aliases
self shadowing
unicode normalization collisions
init=False behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant