Git Product home page Git Product logo

Comments (5)

stalkerg avatar stalkerg commented on July 18, 2024 1

@Dreamsorcerer names like app.py or main.py is very common and can conflicting between lib and application. It's have sense if package can be use as lib or as application.

from aiohttp-devtools.

Dreamsorcerer avatar Dreamsorcerer commented on July 18, 2024

I think that's a bit of an unusual case, but if you can write a test to reproduce that, then it seems like a reasonable change.

from aiohttp-devtools.

venu avatar venu commented on July 18, 2024

Here are the simple steps to reproduce

Folder Structure

libs
  - l1
    - `__init__.py`
    - app.py
app.py

app.py

from aiohttp import web

import sys
sys.path.insert(0, 'libs/l1')

class Main:
    def __init__(self):
        self.app = web.Application()
        self.app.router.add_get('/', self.handle_request)

    async def handle_request(self, request):
        return web.Response(text="testing", content_type='text/html')

def create_app():
    page = Main()
    return page.app

libs/l1/app.py

print("I am app")

Test
adev runserver app.py --port=8080 --aux-port=8086 --verbose --no-livereload

[15:52:30] Root path not specified, using current working directory
[15:52:30] app_path is a file, returning it directly
[15:52:30] config loaded:
Config:
  py_file: PosixPath('*********/test_devtools/app.py')
  static_path: None
  static_url: '/static/'
  livereload: False
  shutdown_by_url: False
  path_prefix: '/_devtools'
  app_factory_name: None
  host: 'localhost'
  main_port: 8080
  aux_port: 8086
[15:52:30] successfully loaded "app" from "*********/test_devtools"
[15:52:30] found default attribute "create_app" in module "<module 'app' from '*********/test_devtools/app.py'>"
[15:52:30] Starting aux server at http://localhost:8086 ◆
[15:52:30] Starting dev server at http://localhost:8080 ●
======== Running on http://0.0.0.0:8086 ========
(Press CTRL+C to quit)
I am app
[15:52:30] successfully loaded "app" from "*********/test_devtools"
Process Process-1:
Traceback (most recent call last):
  File "*********/test_devtools/venv/lib/python3.11/site-packages/aiohttp_devtools/runserver/config.py", line 160, in import_app_factory
    attr = getattr(module, self.app_factory_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'app' has no attribute 'create_app'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/usr/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "*********/test_devtools/venv/lib/python3.11/site-packages/aiohttp_devtools/runserver/serve.py", line 168, in serve_main_app
    app_factory = config.import_app_factory()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "*********/test_devtools/venv/lib/python3.11/site-packages/aiohttp_devtools/runserver/config.py", line 162, in import_app_factory
    raise AdevConfigError("Module '{}' does not define a '{}' attribute/class".format(
aiohttp_devtools.exceptions.AiohttpDevConfigError: Module 'app.py' does not define a 'create_app' attribute/class

from aiohttp-devtools.

Dreamsorcerer avatar Dreamsorcerer commented on July 18, 2024

Here are the simple steps to reproduce

I meant a test, in a PR.

from aiohttp-devtools.

venu avatar venu commented on July 18, 2024

Here is the reproducable test, seems its happening only with process.. so can't mock process in this test.

# test_runserver_main.py

from multiprocessing import set_start_method
from aiohttp_devtools.runserver.watch import AppTask

@forked
def test_start_runserver_with_multi_app_modules(tmpworkdir, event_loop, capfd):
    mktree(tmpworkdir, {
        'app.py': f"""\
from aiohttp import web
import sys
sys.path.insert(0, '{tmpworkdir}/libs/l1')

async def hello(request):
    return web.Response(text='<h1>hello world</h1>', content_type='text/html')

async def create_app():
    a = web.Application()
    a.router.add_get('/', hello)
    return a
""",
        "libs": {
            "l1": {
                "__init__.py": "",
                "app.py": "print('wrong_import')"
            }
        }
    })

    set_start_method('spawn')
    config = Config(app_path="app.py", root_path=tmpworkdir, main_port=0, app_factory_name="create_app")
    config.import_app_factory()
    app_task = AppTask(config)
    
    app_task._start_dev_server()
    app_task._process.join(2)

    captured = capfd.readouterr()
    assert captured.out == "wrong_import\n"

Here is the working test..

from aiohttp-devtools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.