New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: avoid RecursionError
when using some types like Enum
or Literal
with generic models
#2438
fix: avoid RecursionError
when using some types like Enum
or Literal
with generic models
#2438
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2438 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 25 25
Lines 5080 5100 +20
Branches 1041 1048 +7
=========================================
+ Hits 5080 5100 +20
Continue to review full report at Codecov.
|
LGTM, @PrettyWood & @daviskirk I'll merge this soon if you're happy with it. |
Thanks @samuelcolvin ! 🙏 I just updated the change description since it also solves #2454 |
Enum
when combined with generic modelsRecursionError
when using some types with generic models
We should add a test for a literal too, just so we don't forget. |
f419c4c
to
f5c24cb
Compare
RecursionError
when using some types with generic modelsRecursionError
when using some types like Enum
or Literal
with generic models
Looks great to me thanks for fixing my oversights, just tried it out with the literal, works fine as well! @skip_36
def test_generic_literal():
T = TypeVar('T')
class SomeGenericModel(GenericModel, Generic[T]):
some_field: T
class MyModel(BaseModel):
my_gen: SomeGenericModel[Literal["foo"]]
m = MyModel.parse_obj({'my_gen': {'some_field': "foo"}})
assert m.my_gen.some_field == "foo"
with pytest.raises(ValidationError, match=".*permitted: 'foo'.*"):
MyModel.parse_obj({'my_gen': {'some_field': "bar"}}) edit: @PrettyWood, you're really quick! Please Ignore my comment |
🙏 thanks everyone. |
Change Summary
Since an
Enum
is an Iterable, which would cause an infinite loop.Related issue number
closes #2436
closes #2454
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)