fastapi_contrib icon indicating copy to clipboard operation
fastapi_contrib copied to clipboard

Simplify logical expression in Multiple Functions

Open yezz123 opened this issue 4 years ago • 1 comments

  • 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_error with 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.

yezz123 avatar Sep 09 '21 12:09 yezz123

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 any or 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]

yezz123 avatar Sep 09 '21 12:09 yezz123