|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import re |
4 | 3 | import sys |
5 | 4 | from contextlib import nullcontext |
6 | 5 | from itertools import product |
7 | | -from pathlib import PurePath |
8 | 6 | from types import NoneType |
9 | 7 | from typing import Any |
10 | 8 |
|
11 | 9 | import pytest |
12 | | -from hypothesis import example, given, settings |
| 10 | +from hypothesis import given, settings |
13 | 11 | from lxml.etree import Element, QName, _Element |
14 | 12 |
|
15 | 13 | from ._testutils import strategy as _st |
@@ -88,35 +86,18 @@ def test_single_arg_ok(self, arg: Any) -> None: |
88 | 86 | with raise_attr_not_writable: |
89 | 87 | delattr(qn, attr) |
90 | 88 |
|
91 | | - # Path and Exception can stringify to anything, and QName |
92 | | - # construction really succeed with them. Other offending |
93 | | - # types handled in regex below |
94 | | - @settings(max_examples=300) |
95 | | - @given( |
96 | | - thing=_st.all_instances_except_of_type( |
97 | | - str, bytes, QName, _Element, NoneType, PurePath, Exception |
98 | | - ) |
99 | | - ) |
100 | | - @example(thing=bytearray(b"foo")) |
101 | | - def test_single_arg_bad(self, thing: Any) -> None: |
102 | | - if len(str(thing)) == 0 or str(thing).endswith("}"): |
103 | | - raise_cm = pytest.raises(ValueError, match=r"Empty tag name") |
104 | | - elif re.match(r"^[A-Za-z_]([\w\.-])*$", str(thing)): |
105 | | - # UUID, timezones, literal constants etc |
106 | | - raise_cm = nullcontext() |
107 | | - else: |
108 | | - raise_cm = pytest.raises( |
109 | | - ValueError, |
110 | | - match=r"(Invalid tag name|All strings must be XML compatible)", |
111 | | - ) |
112 | | - with raise_cm: |
113 | | - _ = QName(thing) |
114 | | - with raise_cm: |
115 | | - _ = QName(None, thing) |
| 89 | + # Negative test for single arg is removed altogether, as there are too many |
| 90 | + # exceptions. Absurdities like QName(Path("Á")) or QName(TypeError("foo")) |
| 91 | + # works fine, not to mention True, False, Ellipsis etc which stringify to |
| 92 | + # valid XML tag names. |
116 | 93 |
|
117 | 94 | # Proves following rules: |
118 | 95 | # - 2nd arg can't be QName or _Element if 1st non-empty |
119 | | - # - Only 2nd arg can be bytearray when 1st is non-empty |
| 96 | + # - 2nd arg can be bytearray when 1st is non-empty |
| 97 | + # |
| 98 | + # As a side note, supplying bytearray as 1st arg works when 2nd arg is |
| 99 | + # non-empty, but it is almost absolutely certain users don't want to have |
| 100 | + # qualified names like {bytearray(b"foo")}bar |
120 | 101 | @pytest.mark.parametrize( |
121 | 102 | ("arg1", "arg2"), |
122 | 103 | product(arg1_choice, arg2_choice), |
|
0 commit comments