Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
coveragepy/tests/strategies.py at main · coveragepy/coveragepy · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
coveragepy
coveragepy
Repository navigation
Code
Issues
257
(257)
Pull requests
55
(55)
Actions
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
coveragepy
/
tests
/
strategies.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
99 lines (73 loc) · 3.21 KB
main
Breadcrumbs
coveragepy
/
tests
/
strategies.py
Copy path
Top
File metadata and controls
Code
Blame
99 lines (73 loc) · 3.21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/coveragepy/coveragepy/blob/main/NOTICE.txt
"""Hypothesis strategies for use in tests."""
# pragma: enforce 100% coverage
from
__future__
import
annotations
from
collections
.
abc
import
Sequence
from
hypothesis
import
strategies
as
st
# Type aliases:
# S: a Hypothesis strategy, generic.
# P: primitive leaf data types.
# D: nested data.
S
=
st
.
SearchStrategy
P
=
None
|
bool
|
int
|
float
|
str
|
bytes
D
=
P
|
list
[
"D"
]
|
tuple
[
"D"
, ...]
|
set
[
"D"
]
|
dict
[
str
,
"D"
]
# Leaf types, all hashable. Some are limited to the kinds of values coverage
# deals with, to focus Hypothesis.
primitives
:
Sequence
[
S
[
P
]]
=
[
st
.
none
(),
st
.
booleans
(),
st
.
integers
(
min_value
=
-
1000
,
max_value
=
100_000
),
st
.
floats
(
min_value
=
-
100
,
max_value
=
100
,
allow_nan
=
False
,
allow_infinity
=
False
).
map
(
# Avoid -0.0.
lambda
x
:
0.0
if
x
==
0.0
else
x
),
st
.
text
(
max_size
=
10
),
st
.
binary
(
max_size
=
10
),
]
def
homogenous_tuples_of
(
elements
:
S
[
D
])
->
S
[
tuple
[
D
, ...]]:
"""Make a strategy for homogenous tuples.
The elements are all the same type, one of the strategies from `elements`.
The tuples will be varying lengths.
"""
return
st
.
lists
(
elements
,
max_size
=
3
).
map
(
tuple
)
def
true_tuple_strategies
(
element_strategies
:
Sequence
[
S
[
D
]])
->
S
[
S
[
tuple
[
D
, ...]]]:
"""Make a strategy that produces tuple-generating strategies.
The elements will be types drawn from `element_strategies`.
The tuples drawn from the returned strategy will have heterogenous types,
but every tuple will have the same types in the same positions, and all the
tuples will be the same length.
"""
# Note the way we use this, we won't ever get nested tuples: (int, (str, int))
return
st
.
lists
(
st
.
sampled_from
(
element_strategies
),
max_size
=
3
).
map
(
lambda
strats
:
st
.
tuples
(
*
strats
)
)
# A strategy for producing strategies that produce hashable values.
hashable_strategies
:
S
[
S
[
D
]]
=
st
.
one_of
(
st
.
sampled_from
(
primitives
),
true_tuple_strategies
(
primitives
),
)
def
dicts_of
(
leaves
:
S
[
D
])
->
S
[
dict
[
str
,
D
]]:
"""Make a strategy for dicts with string keys and values from `leaves`."""
return
st
.
dictionaries
(
st
.
text
(
max_size
=
10
),
leaves
,
max_size
=
3
)
def
nested_dicts_of
(
leaves
:
S
[
D
])
->
S
[
dict
[
str
,
D
]]:
"""Make a strategy for recursive dicts with leaves from another strategy."""
return
dicts_of
(
st
.
recursive
(
leaves
,
dicts_of
,
max_leaves
=
5
))
# A strategy that generates strategies for nested data.
nested_data_strategies
:
S
[
S
[
D
]]
=
st
.
recursive
(
hashable_strategies
,
lambda
children
:
st
.
one_of
(
children
.
map
(
lambda
s
:
st
.
lists
(
s
,
max_size
=
5
)),
children
.
map
(
homogenous_tuples_of
),
hashable_strategies
.
map
(
lambda
s
:
st
.
sets
(
s
,
max_size
=
5
)),
children
.
map
(
nested_dicts_of
),
),
max_leaves
=
3
,
)
# Use nested_data_strategies to generate data schemas, then use it twice in
# the test to get two chunks of data with the "same shape" but different
# data.
nested_data_pairs
=
nested_data_strategies
.
flatmap
(
lambda
s
:
st
.
tuples
(
s
,
s
))
if
__name__
==
"__main__"
:
for
_
in
range
(
100
):
print
(
repr
(
nested_data_strategies
.
example
().
example
()))
You can’t perform that action at this time.