Skip to content

Commit

Permalink
test(multiclass): Allow tests to be performed conditionally
Browse files Browse the repository at this point in the history
Some tests behave differently for multiclass build; smuggle the build into config variable and pass into tests
  • Loading branch information
abelcheung committed Oct 20, 2024
1 parent 95bf2e0 commit 94a933d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
18 changes: 16 additions & 2 deletions multi-subclass.patch
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
lxml-stubs/html/_element.pyi | 35 ++++++++++--------
lxml-stubs/objectify/_element.pyi | 8 ++--
pyproject.toml | 2 +-
5 files changed, 65 insertions(+), 62 deletions(-)
test-rt/conftest.py | 2 +-
6 files changed, 66 insertions(+), 63 deletions(-)

diff --git a/lxml-stubs/etree/_element.pyi b/lxml-stubs/etree/_element.pyi
index a093bea..445c801 100644
Expand Down Expand Up @@ -317,7 +318,7 @@ index 2b8a301..7fc46e5 100644
expr: str,
*,
diff --git a/pyproject.toml b/pyproject.toml
index 3794c4c..3538d1d 100644
index 94a0848..648d1cd 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -5,7 +5,7 @@ requires = ['pdm-backend ~= 2.3']
Expand All @@ -329,3 +330,16 @@ index 3794c4c..3538d1d 100644
dynamic = ['version']
description = 'Complete lxml external type annotation'
readme = 'README.md'
diff --git a/test-rt/conftest.py b/test-rt/conftest.py
index 27ea129..8e611ce 100644
--- a/test-rt/conftest.py
+++ b/test-rt/conftest.py
@@ -20,7 +20,7 @@ def pytest_collection_finish(session: pytest.Session) -> None:

def pytest_configure(config: pytest.Config) -> None:
# "normal" or "multiclass"
- setattr(config, "types_lxml_build", "normal")
+ setattr(config, "types_lxml_build", "multiclass")


def _bightml_filepath() -> Path:
5 changes: 5 additions & 0 deletions test-rt/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def pytest_collection_finish(session: pytest.Session) -> None:
adapter.run_typechecker_on(files)


def pytest_configure(config: pytest.Config) -> None:
# "normal" or "multiclass"
setattr(config, "types_lxml_build", "normal")


def _bightml_filepath() -> Path:
return Path(__file__).resolve().parent / "data" / "sample.html.xz"

Expand Down
28 changes: 24 additions & 4 deletions test-rt/test_elem_class_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ class MyBaseElement(ElementBase):
count += 1
assert count == 7

def test_single_ns_all_tag_2(self, svg_filepath: pathlib.Path) -> None:
def test_single_ns_all_tag_2(
self, svg_filepath: pathlib.Path, request: pytest.FixtureRequest
) -> None:
"""Class lookup that creates my single element type
for all nodes under specific namespace, while proving
that parser subscript type can't be modified"""
Expand All @@ -101,9 +103,18 @@ class MyBaseElement(ElementBase):
tree = _e.parse(svg_filepath, parser=parser)
root = tree.getroot()

with pytest.raises(TypeCheckError, match="not an instance of .*MyBaseElement"):
if getattr(request.config, "types_lxml_build") == "multiclass":
# For multiclass build, .iter() is hardcoded to return _Element,
# and author is expected to do method / annotation overrides
# in their classes
for e in root.iter():
reveal_type(e)
else:
with pytest.raises(
TypeCheckError, match="not an instance of .*MyBaseElement"
):
for e in root.iter():
reveal_type(e)

def test_single_ns_single_tag_1(self, svg_filepath: pathlib.Path) -> None:
"""Decorator and non-decorator ways of adding per-tag
Expand Down Expand Up @@ -170,7 +181,9 @@ class _EmphClass(ElementBase):
count += 1
assert count == 3

def test_default_ns(self, svg_filepath: pathlib.Path) -> None:
def test_default_ns(
self, svg_filepath: pathlib.Path, request: pytest.FixtureRequest
) -> None:
"""Class lookup that creates my single element type
for all nodes under all namespaces, behaving like
ElementDefaultClassLookup"""
Expand All @@ -192,6 +205,13 @@ class MyBaseElement(ElementBase):
root = tree.getroot()
reveal_type(root)

with pytest.raises(TypeCheckError, match="not an instance of .*MyBaseElement"):
if getattr(request.config, "types_lxml_build") == "multiclass":
# See test_single_ns_all_tag_2 test
for e in root.iter():
reveal_type(e)
else:
with pytest.raises(
TypeCheckError, match="not an instance of .*MyBaseElement"
):
for e in root.iter():
reveal_type(e)

0 comments on commit 94a933d

Please sign in to comment.