fastapi_contrib
fastapi_contrib copied to clipboard
Simplify logical expression in Multiple Functions
- Simplify logical expression using De Morgan identities and Remove redundant conditional in this Function
AuthBackend.authenticate. - Add a guard clause and Convert for loop into dictionary comprehension in this Function
Serializer.update_one. - Refactoring this Function
parse_errorwith these changes :
Merge duplicate blocks in conditional Replace unneeded comprehension with generator Merge else clause's nested if statement into
elif.
- Swap if/else branches and Remove unnecessary else after guard condition in this Function
ValidationErrorLoggingRoute.get_route_handler.custom_route_handler.
https://github.com/identixone/fastapi_contrib/blob/d498c1f3891e96c16b95118853472f3aed885a36/tests/db/test_serializers.py#L455
Relate to this we can simplify the generator
The expression (x for x in [2, 3]) is a generator that returns all of the elements of [2, 3]. If being passed into a function like
anyor all that takes a generator or sequence, it can simply be replaced by [2, 3] which is much clearer.
After simplifying the generator we can use iter:
serializer = TestSerializer(c="2", int_sequence=iter([2, 3]))
assert list(serializer.int_sequence) == [2, 3]