I get error while trying to execute pytest UTs for the project.
E TypeError: 'module' object is not callable
I have the following repository structure:
├───src
│ └───company
│ ├───acc
│ └───dp
│ └───logic
│ ├───business
│ │ ├───__init__.py
│ │ ├───alter_customer.py
│ │ ├───filter_customer.py
│ │ └───review_customers.py
│ └───general
│ ├───some_function.py
│ └───__init__.py
├───tests
│ └───company
│ └───dp
│ └───logic
│ └───business
│ ├───test_alter_customer.py
│ └───test_review_customers.py
├───conftest.py
└───pyproject.toml
Each file under business package contains one function with the same name as the file.
Let's imagine file filter_customer.py like that:
def filter_customer(i: int) -> int:
return i
Let's imagine file review_customers.py like that:
from company.dp.logic.general import some_function
def review_customers() -> str:
x = some_function("custom")
return x
Let's imagine file alter_customer.py like that:
from company.dp.logic.business import filter_customer
def alter_customer() -> str:
x = filter_customer(10) <- this line raise error while trying to run Unit Test
return x
UT looks something like that:
from company.dp.logic.business import alter_customer
def test_alter_customer():
x = alter_customer()
assert x == 10
./business/__init__.py file for business package looks like that:
from company.dp.logic.business.alter_customer import alter_customer
from company.dp.logic.business.filter_customer import filter_customer
from company.dp.logic.business.review_customers import review_customers
./general/__init__.py file for business package looks like that:
from src.company.dp.logic.general.some_function import some_function
pyproject.toml
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[tool.setuptools.packages.find]
where = ["src"]
[tool.pytest.ini_options]
addopts = [
]
testpaths = ["tests/company/dp/logic/business"]
python_files = "test_*.py"
python_functions = "test_*"
pythonpath = "src"
norecursedirs = "*"
How to correctly organize the packages/modules in such scenario?
The goal was to have one function per file and packages like business and general to cover imports from modules.
I have tried multiple versions.
When I use direct import from module/file, it work. It also works when I call function from different package like general.
# This works:
from src.company.dp.logic.business.filter_customer import filter_customer
# With this I have error, even though IDE resolve it correctly:
from src.company.dp.logic.business import filter_customer
# This works:
from src.company.dp.logic.general import some_function
def review_customers()
c = filter_customer(10)
return something
I prepared repository which allows to recreate this problem: https://github.com/paweltajs/module-package/tree/main
Edit. Few other observations.
When I change the name of the function to differ from file name, I got different error:
E ImportError: cannot import name 'f_filter_customer' from partially initialized module 'company.dp.logic.business' (most likely due to a circular import) (C:\Repositories\module-package\src\company\dp\logic\business\__init__.py)
When I put import within the function, it works. Like that:
def alter_customer() -> str:
from company.dp.logic.business import filter_customer
x = filter_customer(10)
return x
srcshouldn't be considered to define a package, just a directory that contains the top-level module/packagecompany.