Maillol / aiohttp-pydantic

Aiohttp View that validates request body and query sting regarding the annotations declared in the View method
MIT License
67 stars 21 forks source link

Improve compatibility with web.View and support subclassing PydanticViews #15

Closed drderuiter closed 3 years ago

drderuiter commented 3 years ago

The changes in this PR make PydanticView structurally a subclass of the aiohttp.web.View. It should make it easier to use PydanticView in places where a regular View is expected. The changes also allow one to inherit from a PydanticView.

Rationale

I was working with aiohttp_pydantic in combination with some other tool, which are designed to work with a regular View. The tool assumed that for non-allowed methods, the handler attribute was missing. This is not the case for the current PydanticView, which sets the raise_not_allowed handler for such methods.

The tool would also subclass the View, which does not work for an PydanticView. Upon subclassing:

Implementation

Alternative solutions

For the inheritance problem, another solution could be to just raise a warning when a subclass of PydanticView is subclassed again. That does not solve the problem, but will let users know that this was not the intended usage.

Curious to hear your thoughts. I'm aware that this is a breaking change as it changes the public interface, but the added compatibility with regular the View should be useful.

codecov-commenter commented 3 years ago

Codecov Report

Merging #15 (c92437c) into main (324c9b0) will decrease coverage by 0.09%. The diff coverage is 93.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #15      +/-   ##
==========================================
- Coverage   92.82%   92.73%   -0.10%     
==========================================
  Files          11       11              
  Lines         683      688       +5     
==========================================
+ Hits          634      638       +4     
- Misses         49       50       +1     
Impacted Files Coverage Δ
aiohttp_pydantic/view.py 97.14% <93.33%> (-1.32%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 324c9b0...c92437c. Read the comment docs.

Maillol commented 3 years ago

I'm afraid that the better way to increase the interoperability whit other tools is to provide a class inherited from object - a mixin - designed to be combined with web.View or subclasses of web.View

aiohttp-pydantic can provide.

PydanticMixin and PydanticView where PydanticView will be defined as:

class PydanticView(PydanticMixin, web.View):
    ...

If the user need to use aiohttp-pydantic with existing View it can do:

class MyExistingViewWithPydantic(PydanticMixin, MyExistingView):
    ...
Maillol commented 3 years ago

Merged with https://github.com/Maillol/aiohttp-pydantic/pull/16 after refactoring