From 276918d658314237409e9c5d633ee0f4f7d0e835 Mon Sep 17 00:00:00 2001 From: HIRANO Satoshi Date: Sat, 29 May 2021 07:31:41 +0900 Subject: [PATCH 1/3] small typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eafdf40..aff8348 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ class ExampleController: self.x = x @controller.route.get( - "/some_aoi", summary="A sample description", response_model=Foo + "/some_api", summary="A sample description", response_model=Foo ) def sample_api(self): print(self.x.bar) # -> amazing_variable From 15a30861f8caa94c20f1c10f1c3e7d045a5eac2b Mon Sep 17 00:00:00 2001 From: KiraPC Date: Tue, 6 Jul 2021 12:18:16 +0200 Subject: [PATCH 2/3] updated fastapi version required and introduced linting --- .flake8 | 12 ++++ .github/workflows/ci.yml | 2 +- .github/workflows/lint.yml | 20 +++++++ .pre-commit-config.yaml | 12 ++++ example/sample_app/app.py | 6 +- .../controller/sample_controller.py | 58 ++++++++++++------- .../controller/sample_controller_2.py | 46 ++++++++++----- example/sample_app_with_auto_import/app.py | 8 ++- .../controller/__init__.py | 2 +- .../controller/sample_controller.py | 50 ++++++++++------ .../libs/sample_parent_controller.py | 13 +++-- fastapi_router_controller/__init__.py | 6 +- fastapi_router_controller/lib/controller.py | 56 ++++++++++-------- .../lib/controller_loader.py | 25 ++++---- fastapi_router_controller/lib/exceptions.py | 5 +- pyproject.toml | 3 + requirements.txt | 2 +- setup.py | 42 +++++++------- tests/test_class_dependencies.py | 12 ++-- tests/test_controller.py | 15 ++--- tests/test_inherit.py | 19 ++++-- 21 files changed, 271 insertions(+), 143 deletions(-) create mode 100644 .flake8 create mode 100644 .github/workflows/lint.yml create mode 100644 .pre-commit-config.yaml create mode 100644 pyproject.toml diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..9723335 --- /dev/null +++ b/.flake8 @@ -0,0 +1,12 @@ +[flake8] +max-line-length = 88 +max-complexity = 10 +exclude = + .git, + __pycache__, + docs/source/conf.py, + old, + build, + dist + src/gunicorn_conf.py + src/catalog/migration.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 15319a4..951a547 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,6 @@ name: Run Tests -on: [push, pull_request] +on: [ pull_request ] jobs: build: diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..0d2e3f8 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,20 @@ +name: Lint +on: [ pull_request ] + +jobs: + lint: + strategy: + fail-fast: false + matrix: + python-version: [ 3.9 ] + os: [ ubuntu-18.04 ] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: flake8 install + run: pip3 install flake8 + - name: lint + run: flake8 fastapi-router-controller/ \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..9dbbc67 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,12 @@ +repos: + - repo: https://github.com/psf/black + rev: 21.5b1 + hooks: + - id: black + args: [ + "fastapi_router_controller" + ] + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.0.0 + hooks: + - id: flake8 \ No newline at end of file diff --git a/example/sample_app/app.py b/example/sample_app/app.py index 8a48384..fba3898 100644 --- a/example/sample_app/app.py +++ b/example/sample_app/app.py @@ -1,13 +1,13 @@ from fastapi import FastAPI -from fastapi.testclient import TestClient from fastapi_router_controller import ControllersTags from controller.sample_controller import SampleController from controller.sample_controller_2 import AnotherSampleController app = FastAPI( - title='A sample application using fastapi_router_controller', + title="A sample application using fastapi_router_controller", version="0.1.0", - openapi_tags=ControllersTags) + openapi_tags=ControllersTags, +) app.include_router(SampleController.router()) app.include_router(AnotherSampleController.router()) diff --git a/example/sample_app/controller/sample_controller.py b/example/sample_app/controller/sample_controller.py index e10eec6..69e3188 100644 --- a/example/sample_app/controller/sample_controller.py +++ b/example/sample_app/controller/sample_controller.py @@ -3,43 +3,61 @@ from fastapi.responses import JSONResponse from pydantic import BaseModel, Field -router = APIRouter(prefix='/sample_controller') +router = APIRouter(prefix="/sample_controller") + +controller = Controller( + router, + openapi_tag={ + "name": "sample_controller", + }, +) -controller = Controller(router, openapi_tag={ - 'name': 'sample_controller', -}) class SampleObject(BaseModel): - id: str = Field(..., description='sample id') + id: str = Field(..., description="sample id") def to_json(self): - return {'id': self.id} + return {"id": self.id} + -class Foo(): +class Foo: def create_foo(_, item): - print('Created Foo', str(item)) + print("Created Foo", str(item)) + def get_foo(): return Foo() -# With the "resource" decorator define the controller Class linked to the Controller router arguments + +# With the "resource" decorator define the controller Class +# linked to the Controller router arguments @controller.resource() -class SampleController(): +class SampleController: def __init__(self, foo: Foo = Depends(get_foo)): self.foo = foo @controller.route.get( - '/', - tags=['sample_controller'], - summary='return a sample object') - def sample_get_request(self, id: str = Query(..., title="itemId", description="The id of the sample object")): - return JSONResponse(status_code=status.HTTP_200_OK, content=SampleObject(**{'id': id}).to_json()) + "/", tags=["sample_controller"], summary="return a sample object" + ) + def sample_get_request( + self, + id: str = Query(..., title="itemId", description="The id of the sample object"), + ): + return JSONResponse( + status_code=status.HTTP_200_OK, content=SampleObject(**{"id": id}).to_json() + ) @controller.route.post( - '/', - tags=['sample_controller'], - summary='create another sample object', - status_code=201) - def sample_post_request(self, simple_object: SampleObject = Body({}, title="SampleObject", description="A sample object model")): + "/", + tags=["sample_controller"], + summary="create another sample object", + status_code=201, + ) + def sample_post_request( + self, + simple_object: SampleObject = Body( + {}, title="SampleObject", description="A sample object model" + ), + ): self.foo.create_foo(simple_object) return JSONResponse(status_code=status.HTTP_201_CREATED, content={}) diff --git a/example/sample_app/controller/sample_controller_2.py b/example/sample_app/controller/sample_controller_2.py index 9656d4f..fb72912 100644 --- a/example/sample_app/controller/sample_controller_2.py +++ b/example/sample_app/controller/sample_controller_2.py @@ -3,29 +3,43 @@ from fastapi.responses import JSONResponse from pydantic import BaseModel, Field -router = APIRouter(prefix='/sample_controller_2') +router = APIRouter(prefix="/sample_controller_2") + +controller = Controller( + router, + openapi_tag={ + "name": "sample_controller_2", + }, +) -controller = Controller(router, openapi_tag={ - 'name': 'sample_controller_2', -}) class SampleObject(BaseModel): - id: str = Field(..., description='sample id') + id: str = Field(..., description="sample id") + -# With the "resource" decorator define the controller Class linked to the Controller router arguments +# With the "resource" decorator define the controller Class +# linked to the Controller router arguments @controller.resource() -class AnotherSampleController(): +class AnotherSampleController: @controller.route.get( - '/', - tags=['sample_controller_2'], - summary='return another sample object') - def sample_get_request(_, id: str = Query(..., title="itemId", description="The id of the sample object")): + "/", tags=["sample_controller_2"], summary="return another sample object" + ) + def sample_get_request( + _, + id: str = Query(..., title="itemId", description="The id of the sample object"), + ): return JSONResponse(status_code=status.HTTP_200_OK, content=SampleObject(id)) @controller.route.post( - '/', - tags=['sample_controller_2'], - summary='create a sample object', - status_code=201) - def sample_post_request(_, simple_object: SampleObject = Body(None, title="SampleObject", description="A sample object model")): + "/", + tags=["sample_controller_2"], + summary="create a sample object", + status_code=201, + ) + def sample_post_request( + _, + simple_object: SampleObject = Body( + None, title="SampleObject", description="A sample object model" + ), + ): return JSONResponse(status_code=status.HTTP_201_CREATED, content={}) diff --git a/example/sample_app_with_auto_import/app.py b/example/sample_app_with_auto_import/app.py index be02c7e..21cb642 100644 --- a/example/sample_app_with_auto_import/app.py +++ b/example/sample_app_with_auto_import/app.py @@ -2,13 +2,15 @@ from fastapi_router_controller import Controller, ControllersTags # just import the main package to load all the controllers in -import controller +import controller # noqa app = FastAPI( - title='A sample application using fastapi_router_controller with controller auto import', + title="A sample application using fastapi_router_controller" + + "with controller auto import", version="0.1.0", # all of the router openapi_tags are collected in ControllerTags object - openapi_tags=ControllersTags) + openapi_tags=ControllersTags, +) for router in Controller.routers(): app.include_router(router) diff --git a/example/sample_app_with_auto_import/controller/__init__.py b/example/sample_app_with_auto_import/controller/__init__.py index 1de5162..5f7802c 100644 --- a/example/sample_app_with_auto_import/controller/__init__.py +++ b/example/sample_app_with_auto_import/controller/__init__.py @@ -4,4 +4,4 @@ this_dir = os.path.dirname(__file__) # load all the module inside the given path -ControllerLoader.load(this_dir, __package__) \ No newline at end of file +ControllerLoader.load(this_dir, __package__) diff --git a/example/sample_app_with_auto_import/controller/sample_controller.py b/example/sample_app_with_auto_import/controller/sample_controller.py index 05d912a..8a4be30 100644 --- a/example/sample_app_with_auto_import/controller/sample_controller.py +++ b/example/sample_app_with_auto_import/controller/sample_controller.py @@ -5,34 +5,50 @@ from libs.sample_parent_controller import SampleParentController -router = APIRouter(prefix='/sample_extended_controller') +router = APIRouter(prefix="/sample_extended_controller") + +controller = Controller( + router, + openapi_tag={ + "name": "sample_extended_controller", + }, +) -controller = Controller(router, openapi_tag={ - 'name': 'sample_extended_controller', -}) class SampleObject(BaseModel): - id: str = Field(..., description='sample id') + id: str = Field(..., description="sample id") def to_json(self): - return {'id': self.id} + return {"id": self.id} + # With the "use" decorator the lib save the Controller Class to load it automatically @controller.use() -# With the "resource" decorator define the controller Class linked to the Controller router arguments +# With the "resource" decorator define the controller Class +# linked to the Controller router arguments @controller.resource() class SampleController(SampleParentController): @controller.route.get( - '/', - tags=['sample_extended_controller'], - summary='return a sample object') - def sample_get_request(self, id: str = Query(..., title="itemId", description="The id of the sample object")): - return JSONResponse(status_code=status.HTTP_200_OK, content=SampleObject(**{'id': id}).to_json()) + "/", tags=["sample_extended_controller"], summary="return a sample object" + ) + def sample_get_request( + self, + id: str = Query(..., title="itemId", description="The id of the sample object"), + ): + return JSONResponse( + status_code=status.HTTP_200_OK, content=SampleObject(**{"id": id}).to_json() + ) @controller.route.post( - '/', - tags=['sample_extended_controller'], - summary='create another sample object', - status_code=201) - def sample_post_request(self, simple_object: SampleObject = Body(None, title="SampleObject", description="A sample object model")): + "/", + tags=["sample_extended_controller"], + summary="create another sample object", + status_code=201, + ) + def sample_post_request( + self, + simple_object: SampleObject = Body( + None, title="SampleObject", description="A sample object model" + ), + ): return JSONResponse(status_code=status.HTTP_201_CREATED, content={}) diff --git a/example/sample_app_with_auto_import/libs/sample_parent_controller.py b/example/sample_app_with_auto_import/libs/sample_parent_controller.py index 645095e..185baca 100644 --- a/example/sample_app_with_auto_import/libs/sample_parent_controller.py +++ b/example/sample_app_with_auto_import/libs/sample_parent_controller.py @@ -6,11 +6,16 @@ controller = Controller(router) + # This is a Sample Controller that can be exteded by another to inherit its routes @controller.resource() -class SampleParentController(): +class SampleParentController: @controller.route.get( - '/parent_api', - summary='A sample API from the extended class, it will inherit the basepat of the child router') + "/parent_api", + summary="A sample API from the extended class," + + "it will inherit the basepat of the child router", + ) def sample_parent_api(_): - return JSONResponse(status_code=status.HTTP_200_OK, content={ 'message': 'I\'m the parent'}) + return JSONResponse( + status_code=status.HTTP_200_OK, content={"message": "I'm the parent"} + ) diff --git a/fastapi_router_controller/__init__.py b/fastapi_router_controller/__init__.py index c8c901a..575bd67 100644 --- a/fastapi_router_controller/__init__.py +++ b/fastapi_router_controller/__init__.py @@ -2,4 +2,8 @@ from fastapi_router_controller.lib.controller import Controller as Controller from fastapi_router_controller.lib.controller import OPEN_API_TAGS as ControllersTags -from fastapi_router_controller.lib.controller_loader import ControllerLoader as ControllerLoader +from fastapi_router_controller.lib.controller_loader import ( + ControllerLoader as ControllerLoader, +) + +__all__ = ["Controller", "ControllersTags", "ControllerLoader"] diff --git a/fastapi_router_controller/lib/controller.py b/fastapi_router_controller/lib/controller.py index dc05a8d..1c80283 100644 --- a/fastapi_router_controller/lib/controller.py +++ b/fastapi_router_controller/lib/controller.py @@ -1,7 +1,10 @@ import inspect from copy import deepcopy from fastapi import APIRouter, Depends -from fastapi_router_controller.lib.exceptions import MultipleResourceException, MultipleRouterException +from fastapi_router_controller.lib.exceptions import ( + MultipleResourceException, + MultipleRouterException, +) OPEN_API_TAGS = [] __app_controllers__ = [] @@ -32,19 +35,21 @@ class Controller: """ - The Controller class. + The Controller class. - It expose some utilities and decorator functions to define a router controller class + It expose some utilities and decorator functions to define a router controller class """ - RC_KEY = '__router__' - SIGNATURE_KEY = '__signature__' - HAS_CONTROLLER_KEY = '__has_controller__' - RESOURCE_CLASS_KEY = '__resource_cls__' + + RC_KEY = "__router__" + SIGNATURE_KEY = "__signature__" + HAS_CONTROLLER_KEY = "__has_controller__" + RESOURCE_CLASS_KEY = "__resource_cls__" def __init__(self, router: APIRouter, openapi_tag: dict = None) -> None: """ - :param router: The FastApi router to link to the Class - :param openapi_tag: An openapi object that will describe your routes in the openapi tamplate + :param router: The FastApi router to link to the Class + :param openapi_tag: An openapi object that will describe your routes + in the openapi tamplate """ # Each Controller must be linked to one fastapi router if hasattr(router, Controller.HAS_CONTROLLER_KEY): @@ -58,10 +63,10 @@ def __init__(self, router: APIRouter, openapi_tag: dict = None) -> None: OPEN_API_TAGS.append(openapi_tag) setattr(router, Controller.HAS_CONTROLLER_KEY, True) - + def __get_parent_routes(self, router: APIRouter): """ - Private utility to get routes from an extended class + Private utility to get routes from an extended class """ for route in router.routes: options = {key: getattr(route, key) for key in __router_params__} @@ -73,14 +78,17 @@ def __get_parent_routes(self, router: APIRouter): self.router.add_api_route(route.path, route.endpoint, **options) def add_resource(self, cls): - ''' - Mark a class as Controller Resource - ''' + """ + Mark a class as Controller Resource + """ # check if the same controller was already used for another cls (Resource) - if hasattr(self, Controller.RESOURCE_CLASS_KEY) and getattr(self, Controller.RESOURCE_CLASS_KEY) != cls: + if ( + hasattr(self, Controller.RESOURCE_CLASS_KEY) + and getattr(self, Controller.RESOURCE_CLASS_KEY) != cls + ): raise MultipleResourceException() - # check if cls (Resource) was exteded from another + # check if cls (Resource) was exteded from another if hasattr(cls, Controller.RC_KEY): self.__get_parent_routes(cls.__router__) @@ -92,13 +100,14 @@ def add_resource(self, cls): def resource(self): """ - A decorator function to mark a Class as a Controller + A decorator function to mark a Class as a Controller """ return self.add_resource def use(_): """ - A decorator function to mark a Class to be automatically loaded by the Controller + A decorator function to mark a Class to be automatically + loaded by the Controller """ def wrapper(cls): @@ -110,7 +119,8 @@ def wrapper(cls): @staticmethod def __parse_controller_router(cls): """ - Private utility to parse the router controller property and extract the correct functions handlers + Private utility to parse the router controller property + and extract the correct functions handlers """ router = getattr(cls, Controller.RC_KEY) @@ -124,7 +134,7 @@ def __parse_controller_router(cls): if dependencies: for depends in dependencies[::-1]: route.dependencies.insert(0, depends) - + # get the signature of the endpoint function signature = inspect.signature(route.endpoint) # get the parameters of the endpoint function @@ -149,7 +159,7 @@ def __parse_controller_router(cls): @staticmethod def routers(): """ - It returns all the Classes marked to be used by the "use" decorator + It returns all the Classes marked to be used by the "use" decorator """ routers = [] @@ -161,7 +171,7 @@ def routers(): @property def route(self) -> APIRouter: """ - It returns the FastAPI router. - Use it as if you are using the original one. + It returns the FastAPI router. + Use it as if you are using the original one. """ return self.router diff --git a/fastapi_router_controller/lib/controller_loader.py b/fastapi_router_controller/lib/controller_loader.py index 8b6a8de..4c0c9d4 100644 --- a/fastapi_router_controller/lib/controller_loader.py +++ b/fastapi_router_controller/lib/controller_loader.py @@ -1,21 +1,24 @@ import os import importlib -class ControllerLoader(): - ''' - The ControllerLoader class. - ''' + +class ControllerLoader: + """ + The ControllerLoader class. + """ + @staticmethod def load(directory, package): - ''' - It is an utility to load automatically all the python module presents on a given directory - ''' + """ + It is an utility to load automatically all the python + module presents on a given directory + """ for module in os.listdir(directory): - sub_dir = directory + '/' + module + sub_dir = directory + "/" + module if os.path.isdir(sub_dir): - ControllerLoader.load(sub_dir, '{}.{}'.format(package, module)) - if module == '__init__.py' or module[-3:] != '.py': + ControllerLoader.load(sub_dir, "{}.{}".format(package, module)) + if module == "__init__.py" or module[-3:] != ".py": continue else: - module_import_name = '{}.{}'.format(package, module[:-3]) + module_import_name = "{}.{}".format(package, module[:-3]) importlib.import_module(module_import_name) diff --git a/fastapi_router_controller/lib/exceptions.py b/fastapi_router_controller/lib/exceptions.py index 112d572..260e1ae 100644 --- a/fastapi_router_controller/lib/exceptions.py +++ b/fastapi_router_controller/lib/exceptions.py @@ -1,7 +1,8 @@ class MultipleRouterException(Exception): def __init__(self): - super().__init__('Router already used by another Controller') + super().__init__("Router already used by another Controller") + class MultipleResourceException(Exception): def __init__(self): - super().__init__('Controller already used by another Resource') + super().__init__("Controller already used by another Resource") diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..2605e0e --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[tool.black] +line-length = 88 +target-version = ['py36', 'py37', 'py38'] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 8b56413..7918312 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -fastapi==0.63.0 \ No newline at end of file +fastapi>=0.63.0 \ No newline at end of file diff --git a/setup.py b/setup.py index 8695061..2ecebd1 100644 --- a/setup.py +++ b/setup.py @@ -1,38 +1,40 @@ -''' +""" :author: Pasquale Carmine Carbone Setup module -''' +""" import setuptools -with open('README.md', 'r') as fh: +with open("README.md", "r") as fh: LONG_DESCRIPTION = fh.read() -with open('requirements.txt', 'r') as fin: +with open("requirements.txt", "r") as fin: REQS = fin.read().splitlines() -VERSION = '__VERSION__' +VERSION = "__VERSION__" setuptools.setup( version=VERSION, - name='fastapi-router-controller', - author='Pasquale Carmine Carbone', - author_email='pasqualecarmine.carbone@gmail.com', - description='A FastAPI utility to allow Controller Class usage', + name="fastapi-router-controller", + author="Pasquale Carmine Carbone", + author_email="pasqualecarmine.carbone@gmail.com", + description="A FastAPI utility to allow Controller Class usage", long_description=LONG_DESCRIPTION, - url='https://github.com/KiraPC/fastapi-router-controller', - long_description_content_type='text/markdown', - packages=setuptools.find_packages(exclude=['venv', 'fastapi-router-controller.egg-info', 'build']), + url="https://github.com/KiraPC/fastapi-router-controller", + long_description_content_type="text/markdown", + packages=setuptools.find_packages( + exclude=["venv", "fastapi-router-controller.egg-info", "build"] + ), classifiers=[ - 'Programming Language :: Python :: 3', - 'License :: OSI Approved :: MIT License', - 'Operating System :: OS Independent', - 'Topic :: Software Development :: Libraries :: Python Modules', - 'Topic :: Software Development :: Libraries', - 'Topic :: Software Development', - 'Typing :: Typed', + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + "Topic :: Software Development :: Libraries :: Python Modules", + "Topic :: Software Development :: Libraries", + "Topic :: Software Development", + "Typing :: Typed", ], - python_requires='>=3.6', + python_requires=">=3.6", install_requires=REQS, extras_require={"tests": ["pytest", "pytest-cov", "requests"]}, ) diff --git a/tests/test_class_dependencies.py b/tests/test_class_dependencies.py index c7269c9..1fc8644 100644 --- a/tests/test_class_dependencies.py +++ b/tests/test_class_dependencies.py @@ -40,16 +40,16 @@ def setUp(self): self.client = TestClient(app) def test_class_dep(self): - response = self.client.get('/users/1') + response = self.client.get("/users/1") self.assertEqual(response.status_code, 400) - self.assertEqual(response.json(), {'detail': 'No User'}) + self.assertEqual(response.json(), {"detail": "No User"}) def test_func_dep(self): - response = self.client.get('/users/6') + response = self.client.get("/users/6") self.assertEqual(response.status_code, 400) - self.assertEqual(response.json(), {'detail': 'Not exact user'}) + self.assertEqual(response.json(), {"detail": "Not exact user"}) def test_pass(self): - response = self.client.get('/users/7') + response = self.client.get("/users/7") self.assertEqual(response.status_code, 200) - self.assertEqual(response.json(), {'user_id': 7}) + self.assertEqual(response.json(), {"user_id": 7}) diff --git a/tests/test_controller.py b/tests/test_controller.py index 4785448..423b879 100644 --- a/tests/test_controller.py +++ b/tests/test_controller.py @@ -33,7 +33,8 @@ def create_app_declerative(): router = APIRouter() controller = Controller(router, openapi_tag={"name": "sample_controller"}) - # With the 'resource' decorator define the controller Class linked to the Controller router arguments + # With the 'resource' decorator define the controller Class + # linked to the Controller router arguments @controller.resource() class SampleController: def __init__(self, x=Depends(get_x)): @@ -55,7 +56,8 @@ def root( return SampleObject(id=id) @controller.route.post( - "/hello", response_model=SampleObject, + "/hello", + response_model=SampleObject, ) def hello(self, f: Filter, y=Depends(get_y)): _id = f.foo @@ -87,9 +89,7 @@ def root( id += self.x.create() return SampleObject(id=id) - def hello(self, - f: Filter, y=Depends(get_y) - ): + def hello(self, f: Filter, y=Depends(get_y)): _id = f.foo _id += y _id += self.x.create() @@ -110,10 +110,7 @@ def hello(self, ) controller.route.add_api_route( - "/hello", - SampleController.hello, - response_model=SampleObject, - methods=["POST"] + "/hello", SampleController.hello, response_model=SampleObject, methods=["POST"] ) app = FastAPI( diff --git a/tests/test_inherit.py b/tests/test_inherit.py index e1cd282..aa6460e 100644 --- a/tests/test_inherit.py +++ b/tests/test_inherit.py @@ -34,13 +34,16 @@ def __init__(self): self.bla = "foo" @parent_controller.route.get( - "/hambu", tags=["parent"], response_model=Object, + "/hambu", + tags=["parent"], + response_model=Object, ) def hambu(self): return Object(id="hambu-%s" % self.bla) -# With the 'resource' decorator define the controller Class linked to the Controller router arguments +# With the 'resource' decorator define the controller +# Class linked to the Controller router arguments @child_controller.resource() class Controller(Base): def __init__(self, x=Depends(get_x)): @@ -48,10 +51,14 @@ def __init__(self, x=Depends(get_x)): self.x = x @child_controller.route.get( - "/", tags=["child"], summary="return a object", response_model=Object, + "/", + tags=["child"], + summary="return a object", + response_model=Object, ) def root( - self, id: str = Query(..., title="itemId", description="The id of the object"), + self, + id: str = Query(..., title="itemId", description="The id of the object"), ): id += self.x.create() + self.bla return Object(id=id) @@ -92,4 +99,6 @@ def test_invalid(self): class Controller2(Base): ... - self.assertEqual(str(ex.exception), "Controller already used by another Resource") + self.assertEqual( + str(ex.exception), "Controller already used by another Resource" + ) From c0833aaec6bdafb9d8c72aa95ae0c425b7bdf6b4 Mon Sep 17 00:00:00 2001 From: KiraPC Date: Tue, 6 Jul 2021 12:25:03 +0200 Subject: [PATCH 3/3] fix lint ci --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 0d2e3f8..39e6496 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -17,4 +17,4 @@ jobs: - name: flake8 install run: pip3 install flake8 - name: lint - run: flake8 fastapi-router-controller/ \ No newline at end of file + run: flake8 fastapi_router_controller/ \ No newline at end of file