Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita331d3e

Browse files
authored
Merge pull request#3768 from tybug/namedtuple-generic
2 parentsb4ddfd1 +0d0ff88 commita331d3e

File tree

5 files changed

+58
-5
lines changed

5 files changed

+58
-5
lines changed

‎AUTHORS.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ their individual contributions.
108108
* `Lampros Mountrakis<https://www.github.com/lmount>`_
109109
* `Lea Provenzano<https://github.com/leaprovenzano>`_
110110
* `Lee Begg<https://www.github.com/llnz2>`_
111+
* `Liam DeVoe<https://github.com/tybug>`_
111112
* `Libor Martínek<https://github.com/bibajz>`_
112113
* `Lisa Goeller<https://www.github.com/lgoeller>`_
113114
* `Louis Taylor<https://github.com/kragniz>`_

‎hypothesis-python/RELEASE.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch improves:func:`~hypothesis.strategies.register_type_strategy` when used with ``tuple`` subclasses,
4+
by preventing them from being interpreted as generic and provided to strategies like ``st.from_type(Sequence[int])``
5+
(:issue:`3767`).

‎hypothesis-python/src/hypothesis/strategies/_internal/types.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,16 @@ def from_typing_type(thing):
391391
fork,vin_global_type_lookup.items()
392392
ifis_generic_type(k)andtry_issubclass(k,thing)
393393
}
394-
# Drop some unusual cases for simplicity
395-
forweirdin (tuple,getattr(os,"_Environ",None)):
396-
iflen(mapping)>1:
397-
mapping.pop(weird,None)
394+
# Drop some unusual cases for simplicity, including tuples or its
395+
# subclasses (e.g. namedtuple)
396+
iflen(mapping)>1:
397+
_Environ=getattr(os,"_Environ",None)
398+
mapping.pop(_Environ,None)
399+
tuple_types= [tfortinmappingifisinstance(t,type)andissubclass(t,tuple)]
400+
iflen(mapping)>len(tuple_types):
401+
fortuple_typeintuple_types:
402+
mapping.pop(tuple_type)
403+
398404
# After we drop Python 3.8 and can rely on having generic builtin types, we'll
399405
# be able to simplify this logic by dropping the typing-module handling.
400406
if {dict,set,typing.Dict,typing.Set}.intersection(mapping):
@@ -407,6 +413,7 @@ def from_typing_type(thing):
407413
# the ghostwriter than it's worth, via undefined names in the repr.
408414
mapping.pop(collections.deque,None)
409415
mapping.pop(typing.Deque,None)
416+
410417
iflen(mapping)>1:
411418
# issubclass treats bytestring as a kind of sequence, which it is,
412419
# but treating it as such breaks everything else when it is presumed

‎hypothesis-python/tests/cover/test_lookup.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@
3636
fromhypothesis.strategiesimportfrom_type
3737
fromhypothesis.strategies._internalimporttypes
3838

39-
fromtests.common.debugimportassert_all_examples,find_any,minimal
39+
fromtests.common.debugimport (
40+
assert_all_examples,
41+
assert_no_examples,
42+
find_any,
43+
minimal,
44+
)
4045
fromtests.common.utilsimportfails_with,temp_registered
4146

4247
sentinel=object()
@@ -1146,6 +1151,17 @@ def f(x: int):
11461151
st.builds(f).example()
11471152

11481153

1154+
classTupleSubtype(tuple):
1155+
pass
1156+
1157+
1158+
deftest_tuple_subclasses_not_generic_sequences():
1159+
# see https://github.com/HypothesisWorks/hypothesis/issues/3767.
1160+
withtemp_registered(TupleSubtype,st.builds(TupleSubtype)):
1161+
s=st.from_type(typing.Sequence[int])
1162+
assert_no_examples(s,lambdax:isinstance(x,tuple))
1163+
1164+
11491165
deftest_custom_strategy_function_resolves_types_conditionally():
11501166
sentinel=object()
11511167

‎hypothesis-python/tests/cover/test_lookup_py39.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,27 @@ def test_can_register_builtin_list():
125125
st.from_type(list[int]),
126126
lambdals:len(ls)<=2and {type(x)forxinls}.issubset({int}),
127127
)
128+
129+
130+
T=typing.TypeVar("T")
131+
132+
133+
@typing.runtime_checkable
134+
classFooable(typing.Protocol[T]):
135+
deffoo(self):
136+
...
137+
138+
139+
classFooableConcrete(tuple):
140+
deffoo(self):
141+
pass
142+
143+
144+
deftest_only_tuple_subclasses_in_typing_type():
145+
# A generic typing type (such as Fooable) whose only concrete
146+
# instantiations are tuples should still generate tuples. This is in
147+
# contrast to test_tuple_subclasses_not_generic_sequences, which discards
148+
# tuples if there are any alternatives.
149+
withtemp_registered(FooableConcrete,st.builds(FooableConcrete)):
150+
s=st.from_type(Fooable[int])
151+
assert_all_examples(s,lambdax:type(x)isFooableConcrete)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp