Skip to content

Commit

Permalink
Cleans up the tree and fixes doctest ImportError (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpshelio authored Nov 18, 2024
1 parent 725ea72 commit eed1dce
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions ch04packaging/03Packaging.ipynb.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,23 @@
#
# ```
# repository_name
# |-- src
# | `-- package_name
# | |-- __init__.py # optional; required for exporting things under package's namespace
# | |-- python_file.py
# | |-- another_python_file.py
# `-- tests
# |-- fixtures
# | `-- fixture_file.yaml
# `-- test_python_file.py
# |-- LICENSE.md
# |-- CITATION.md
# |-- README.md
# `-- pyproject.toml
# ├── src
# │   └── package_name
# │      ├── __init__.py # optional; required for exporting things under package's namespace
# │      ├── python_file.py
# │   └── another_python_file.py
# ├── tests
# │ ├── fixtures
# │ │   └── fixture_file.yaml
# │   └── test_python_file.py
# ├── docs
# │ ├── index.rst
# │ ├── conf.py
# │   └── ...
# ├── LICENSE.md
# ├── CITATION.md
# ├── README.md
# └── pyproject.toml
# ```
#
#
Expand Down Expand Up @@ -546,20 +550,14 @@ def process():
# %%
# %%writefile greetings_repo/tests/test_greeter.py

import os
from pathlib import Path

import yaml

from greetings.greeter import greet

def test_greet():
with open(
os.path.join(
os.path.dirname(__file__),
'fixtures',
'samples.yaml'
)
) as fixtures_file:
with open(Path(__file__).parent / 'fixtures' / 'samples.yaml') as fixtures_file:
fixtures = yaml.safe_load(fixtures_file)
for fixture in fixtures:
answer = fixture.pop('answer')
Expand Down Expand Up @@ -607,21 +605,15 @@ def test_greet():
# %%
# %%writefile greetings_repo/tests/test_greeter.py

import os
from pathlib import Path

import pytest
import yaml

from greetings.greeter import greet

def read_fixture():
with open(
os.path.join(
os.path.dirname(__file__),
'fixtures',
'samples.yaml'
)
) as fixtures_file:
with open(Path(__file__).parent / 'fixtures' / 'samples.yaml') as fixtures_file:
fixtures = yaml.safe_load(fixtures_file)
return fixtures

Expand All @@ -647,6 +639,15 @@ def test_greeter(fixture):
# cd greetings_repo
# pytest --doctest-modules

# %% [markdown]
# The `ImportError` appears here `pytest` requires `__init__.py` files to discover test cases when using relative imports (though the library works fine without it).

# %% magic_args="--no-raise-error" language="bash"
#
# cd greetings_repo
# touch src/greetings/__init__.py
# pytest --doctest-modules

# %% [markdown]
# Finally, we typically don't want to include the tests when we distribute our software for our users.
# We can also add pytest as an "optional" dependency for the developers of our package.
Expand Down

0 comments on commit eed1dce

Please sign in to comment.