Skip to content

refactor(email): one definition of email_questions - #136

Merged
NandhaKishorM merged 2 commits into
NandhaKishorM:mainfrom
LudwigJMarx:refactor/email-questions-one-definition
Sep 23, 2026
Merged

NandhaKishorM merged 2 commits into
NandhaKishorM:mainfrom
LudwigJMarx:refactor/email-questions-one-definition

Conversation

@LudwigJMarx

Copy link
Copy Markdown
Contributor

Problem

email_questions is defined twice, in laya/email.py:70 and in laya/presets.py:45, with
identical bodies and different docstrings. laya/__init__.py:17 re-exports the presets one,
so laya.email_questions and laya.email.email_questions are two objects that happen to agree.

They can stop agreeing with nothing noticing. Adding a legal category to the copy in
laya/email.py:

>>> laya.email_questions()["category"]["criteria"]        # no 'legal'
>>> laya.email.email_questions()["category"]["criteria"]  # 'legal'

and the checks do not react:

check result with the two copies out of sync
tests/test_email.py exit 0
tests/test_criteria.py exit 0
tests/test_router.py exit 0
tests/test_packaging.py exit 0
ruff, the CI selection All checks passed

So an edit to that copy is invisible to the documented API and to everything the project runs.

Environment

commit   573e5b6
python   3.14.7 (Homebrew), macOS 25.6.0 (arm64)
setup    python3 -m venv .venv && .venv/bin/pip install -e .

Failing test

tests/test_email.py gains the invariant that both module paths are one object. Against the
unchanged package:

$ python tests/test_email.py

14 passed, 1 failed
  FAIL email_questions/one definition behind both module paths
       laya.email.email_questions is not laya.presets.email_questions
$ echo $?
1

Three checks sit next to it and pass in both states: the package export is that same object,
both paths answer the same, and a caller override reaches both paths. They hold the behaviour
while the identity check holds the shape.

Solution

+33 / -37 across two files. laya/email.py drops its copy and re-exports the one in
laya/presets.py:

# One definition, in the module that holds the other presets. Re-exported here because
# `from laya.email import email_questions` is a path callers already have.
from .presets import email_questions  # noqa: F401

presets.py owns the other four presets and is what __init__ already imports from, so the
definition stays there.

Rejected: deleting the copy outright. Smaller diff, but it removes a public import path from a
library at 0.3.5 with no deprecation channel, and the re-export costs one line.

Rejected the other way round, moving the definition into laya/email.py and importing it from
presets: that splits the five presets across two modules and leaves __init__ importing four
names from one and one from the other.

No import cycle. laya/presets.py imports only from typing, and all three entry points work:

$ python -c "import laya.email, laya.presets"
$ python -c "import laya.presets, laya.email"
$ python -c "from laya.email import email_questions"

Evidence

$ python tests/test_email.py          # after

15 passed, 0 failed
$ echo $?
0

With one definition the same mutation reaches both paths:

>>> # 'legal' added to laya/presets.py
>>> 'legal' in laya.email_questions()["category"]["criteria"]        # True
>>> 'legal' in laya.email.email_questions()["category"]["criteria"]  # True

The rest of the suite on 573e5b6, before and after, identical:

file result
tests/test_router.py all routing tests passed
tests/test_criteria.py 34 passed, 0 failed
tests/test_download.py Ran 4 tests, OK
tests/test_shortlist.py 81 passed, 0 failed
tests/test_decision_model.py all decision model tests passed
tests/test_packaging.py all packaging tests passed

ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120 and
python -m compileall -q laya/ tests/ clean. python -c "import laya; print(laya.__version__); print(sorted(laya.__all__))", the CI import step, lists the same 27 names and all resolve.

Note

This branch is cut from 573e5b6 and does not depend on #132 or #133. It touches
tests/test_email.py, which #132 also extends, in a different place; whichever lands second
rebases.

@NandhaKishorM

Copy link
Copy Markdown
Owner

Thank you, two definitions of email_questions that can silently drift is worth removing. Today's main moved a lot (35 PRs merged), so this now conflicts. Could you rebase onto the latest main?

`email_questions` had two definitions, in `laya/email.py` and in
`laya/presets.py`. The bodies were identical and the docstrings were not.
`laya/__init__.py` re-exports the `presets` one. So `laya.email_questions` and
`laya.email.email_questions` were two objects that happened to agree.

They can stop agreeing, and nothing notices. Measured on 010bace, Python
3.14.7, macOS 25.6.0. Add a `legal` category to the copy in `laya/email.py`:

    laya.email_questions()        -> no 'legal'
    laya.email.email_questions()  -> 'legal'

All 16 test files in the CI list exit 0, and ruff is clean. An edit to that
copy is invisible to the documented API and to every check the project runs.

The presets module holds the other four presets, and `__init__` already imports
from it. So the definition stays there. `laya/email.py` re-exports it, because
`from laya.email import email_questions` is a path callers already have.

Rejected:

* Delete the copy outright. The diff is smaller, but it removes a public import
  path from a library at 0.3.x that has no deprecation channel.
* Move the definition into `laya/email.py` and import it in `presets`. That
  splits the five presets across two modules. `__init__` would then import four
  names from one module and one name from the other.

`tests/test_email.py` gains the invariant: both module paths are one object.
On 010bace it is 49 passed / 1 failed before and 50 passed / 0 failed after.
First measured on 573e5b6: 14 / 1 before, 15 / 0 after.

Three checks next to it pass in both states: the package export, equal output,
and a caller override that reaches both paths. They hold the behaviour. The
identity check holds the shape.

With one definition, the same mutation reaches both paths.

No import cycle. `laya/presets.py` imports only from `typing`. Checked in both
orders and on the direct import. The 16 test files in the CI list, ruff and
compileall all pass after the change.
@LudwigJMarx
LudwigJMarx force-pushed the refactor/email-questions-one-definition branch from 095f9f7 to fc8af4d Compare September 23, 2026 08:36
@LudwigJMarx

Copy link
Copy Markdown
Contributor Author

Rebased onto 010bace. Same conflict as #132: both sides append to tests/test_email.py, and both blocks are kept.

tests/test_email.py on 010bace result
without this change 49 passed, 1 failed
with this change 50 passed, 0 failed

The drift is still there on 010bace, so I measured it again. I added a category to the copy in laya/email.py. laya.email.email_questions() returns it and laya.email_questions() does not. All 16 test files in the CI list still exit 0, and ruff is clean. The two definitions are at laya/email.py:172 and laya/presets.py:45.

# Conflicts:
#	tests/test_email.py
@NandhaKishorM

Copy link
Copy Markdown
Owner

Thank you for rebasing. After #227 landed both PRs appended to tests/test_email.py, so I resolved that one conflict on your branch by keeping both blocks. Your drift demonstration (a category added to one copy and missing from the other) is exactly why one definition is better. Merging.

@NandhaKishorM
NandhaKishorM merged commit 4419a01 into NandhaKishorM:main Sep 23, 2026
14 checks passed
baninaveen pushed a commit to baninaveen/laya that referenced this pull request Sep 24, 2026
`email_questions` had two definitions, in `laya/email.py` and in
`laya/presets.py`. The bodies were identical and the docstrings were not.
`laya/__init__.py` re-exports the `presets` one. So `laya.email_questions` and
`laya.email.email_questions` were two objects that happened to agree.

They can stop agreeing, and nothing notices. Measured on 010bace, Python
3.14.7, macOS 25.6.0. Add a `legal` category to the copy in `laya/email.py`:

    laya.email_questions()        -> no 'legal'
    laya.email.email_questions()  -> 'legal'

All 16 test files in the CI list exit 0, and ruff is clean. An edit to that
copy is invisible to the documented API and to every check the project runs.

The presets module holds the other four presets, and `__init__` already imports
from it. So the definition stays there. `laya/email.py` re-exports it, because
`from laya.email import email_questions` is a path callers already have.

Rejected:

* Delete the copy outright. The diff is smaller, but it removes a public import
  path from a library at 0.3.x that has no deprecation channel.
* Move the definition into `laya/email.py` and import it in `presets`. That
  splits the five presets across two modules. `__init__` would then import four
  names from one module and one name from the other.

`tests/test_email.py` gains the invariant: both module paths are one object.
On 010bace it is 49 passed / 1 failed before and 50 passed / 0 failed after.
First measured on 573e5b6: 14 / 1 before, 15 / 0 after.

Three checks next to it pass in both states: the package export, equal output,
and a caller override that reaches both paths. They hold the behaviour. The
identity check holds the shape.

With one definition, the same mutation reaches both paths.

No import cycle. `laya/presets.py` imports only from `typing`. Checked in both
orders and on the direct import. The 16 test files in the CI list, ruff and
compileall all pass after the change.

Co-authored-by: Nandakishor <48623612+NandhaKishorM@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants