Redundant `isinstance` checks generated for unions where every member is a model · openapi-generators/openapi-python-client · Discussion #1486 · GitHub
When a property is a union of schemas that are all object/model types (e.g. oneOf: [ModelA, ModelB]), the generated to_dict() method checks the type of the value with isinstance before converting it, even though every possible type is converted the exact same way (.to_dict()). The type check doesn't actually change anything, since every generated model class is converted identically.
This makes the generated code harder to read than necessary, and trips up linters (e.g. ruff flags it when using the SIM rules).
Note: This only applies when every member of the union is a model. Unions that mix models with primitives/enums must keep their existing per-member check, since those really do need different handling.
reacted with thumbs up emoji
reacted with thumbs down emoji
reacted with laugh emoji
reacted with hooray emoji
reacted with confused emoji
reacted with heart emoji
reacted with rocket emoji
reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
{{title}}
Uh oh!
There was an error while loading. Please reload this page.
When a property is a union of schemas that are all object/model types (e.g. oneOf: [ModelA, ModelB]), the generated to_dict() method checks the type of the value with isinstance before converting it, even though every possible type is converted the exact same way (.to_dict()). The type check doesn't actually change anything, since every generated model class is converted identically.
This makes the generated code harder to read than necessary, and trips up linters (e.g. ruff flags it when using the SIM rules).
Generated today:
Expected:
Note: This only applies when every member of the union is a model. Unions that mix models with primitives/enums must keep their existing per-member check, since those really do need different handling.