Matt300209 commited on
Commit
f6659ea
·
verified ·
1 Parent(s): 2e550fa

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. venv/lib/python3.10/site-packages/MarkupSafe-3.0.2.dist-info/INSTALLER +1 -0
  2. venv/lib/python3.10/site-packages/MarkupSafe-3.0.2.dist-info/LICENSE.txt +28 -0
  3. venv/lib/python3.10/site-packages/MarkupSafe-3.0.2.dist-info/METADATA +92 -0
  4. venv/lib/python3.10/site-packages/MarkupSafe-3.0.2.dist-info/RECORD +14 -0
  5. venv/lib/python3.10/site-packages/MarkupSafe-3.0.2.dist-info/WHEEL +6 -0
  6. venv/lib/python3.10/site-packages/MarkupSafe-3.0.2.dist-info/top_level.txt +1 -0
  7. venv/lib/python3.10/site-packages/absl_py-2.3.1.dist-info/INSTALLER +1 -0
  8. venv/lib/python3.10/site-packages/absl_py-2.3.1.dist-info/METADATA +101 -0
  9. venv/lib/python3.10/site-packages/absl_py-2.3.1.dist-info/RECORD +53 -0
  10. venv/lib/python3.10/site-packages/absl_py-2.3.1.dist-info/WHEEL +4 -0
  11. venv/lib/python3.10/site-packages/absl_py-2.3.1.dist-info/licenses/AUTHORS +7 -0
  12. venv/lib/python3.10/site-packages/absl_py-2.3.1.dist-info/licenses/LICENSE +202 -0
  13. venv/lib/python3.10/site-packages/attr/__init__.py +104 -0
  14. venv/lib/python3.10/site-packages/attr/__init__.pyi +389 -0
  15. venv/lib/python3.10/site-packages/attr/__pycache__/__init__.cpython-310.pyc +0 -0
  16. venv/lib/python3.10/site-packages/attr/__pycache__/_cmp.cpython-310.pyc +0 -0
  17. venv/lib/python3.10/site-packages/attr/__pycache__/_compat.cpython-310.pyc +0 -0
  18. venv/lib/python3.10/site-packages/attr/__pycache__/_config.cpython-310.pyc +0 -0
  19. venv/lib/python3.10/site-packages/attr/__pycache__/_funcs.cpython-310.pyc +0 -0
  20. venv/lib/python3.10/site-packages/attr/__pycache__/_make.cpython-310.pyc +0 -0
  21. venv/lib/python3.10/site-packages/attr/__pycache__/_next_gen.cpython-310.pyc +0 -0
  22. venv/lib/python3.10/site-packages/attr/__pycache__/_version_info.cpython-310.pyc +0 -0
  23. venv/lib/python3.10/site-packages/attr/__pycache__/converters.cpython-310.pyc +0 -0
  24. venv/lib/python3.10/site-packages/attr/__pycache__/exceptions.cpython-310.pyc +0 -0
  25. venv/lib/python3.10/site-packages/attr/__pycache__/filters.cpython-310.pyc +0 -0
  26. venv/lib/python3.10/site-packages/attr/__pycache__/setters.cpython-310.pyc +0 -0
  27. venv/lib/python3.10/site-packages/attr/__pycache__/validators.cpython-310.pyc +0 -0
  28. venv/lib/python3.10/site-packages/attr/_cmp.py +160 -0
  29. venv/lib/python3.10/site-packages/attr/_cmp.pyi +13 -0
  30. venv/lib/python3.10/site-packages/attr/_compat.py +94 -0
  31. venv/lib/python3.10/site-packages/attr/_config.py +31 -0
  32. venv/lib/python3.10/site-packages/attr/_funcs.py +468 -0
  33. venv/lib/python3.10/site-packages/attr/_make.py +3123 -0
  34. venv/lib/python3.10/site-packages/attr/_next_gen.py +623 -0
  35. venv/lib/python3.10/site-packages/attr/_typing_compat.pyi +15 -0
  36. venv/lib/python3.10/site-packages/attr/_version_info.py +86 -0
  37. venv/lib/python3.10/site-packages/attr/_version_info.pyi +9 -0
  38. venv/lib/python3.10/site-packages/attr/converters.py +162 -0
  39. venv/lib/python3.10/site-packages/attr/converters.pyi +19 -0
  40. venv/lib/python3.10/site-packages/attr/exceptions.py +95 -0
  41. venv/lib/python3.10/site-packages/attr/exceptions.pyi +17 -0
  42. venv/lib/python3.10/site-packages/attr/filters.py +72 -0
  43. venv/lib/python3.10/site-packages/attr/filters.pyi +6 -0
  44. venv/lib/python3.10/site-packages/attr/py.typed +0 -0
  45. venv/lib/python3.10/site-packages/attr/setters.py +79 -0
  46. venv/lib/python3.10/site-packages/attr/setters.pyi +20 -0
  47. venv/lib/python3.10/site-packages/attr/validators.py +710 -0
  48. venv/lib/python3.10/site-packages/attr/validators.pyi +86 -0
  49. venv/lib/python3.10/site-packages/glom/__init__.py +54 -0
  50. venv/lib/python3.10/site-packages/glom/__main__.py +4 -0
venv/lib/python3.10/site-packages/MarkupSafe-3.0.2.dist-info/INSTALLER ADDED
@@ -0,0 +1 @@
 
 
1
+ pip
venv/lib/python3.10/site-packages/MarkupSafe-3.0.2.dist-info/LICENSE.txt ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Copyright 2010 Pallets
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions are
5
+ met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright
8
+ notice, this list of conditions and the following disclaimer.
9
+
10
+ 2. Redistributions in binary form must reproduce the above copyright
11
+ notice, this list of conditions and the following disclaimer in the
12
+ documentation and/or other materials provided with the distribution.
13
+
14
+ 3. Neither the name of the copyright holder nor the names of its
15
+ contributors may be used to endorse or promote products derived from
16
+ this software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21
+ PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24
+ TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
venv/lib/python3.10/site-packages/MarkupSafe-3.0.2.dist-info/METADATA ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Metadata-Version: 2.1
2
+ Name: MarkupSafe
3
+ Version: 3.0.2
4
+ Summary: Safely add untrusted strings to HTML/XML markup.
5
+ Maintainer-email: Pallets <contact@palletsprojects.com>
6
+ License: Copyright 2010 Pallets
7
+
8
+ Redistribution and use in source and binary forms, with or without
9
+ modification, are permitted provided that the following conditions are
10
+ met:
11
+
12
+ 1. Redistributions of source code must retain the above copyright
13
+ notice, this list of conditions and the following disclaimer.
14
+
15
+ 2. Redistributions in binary form must reproduce the above copyright
16
+ notice, this list of conditions and the following disclaimer in the
17
+ documentation and/or other materials provided with the distribution.
18
+
19
+ 3. Neither the name of the copyright holder nor the names of its
20
+ contributors may be used to endorse or promote products derived from
21
+ this software without specific prior written permission.
22
+
23
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26
+ PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29
+ TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ Project-URL: Donate, https://palletsprojects.com/donate
36
+ Project-URL: Documentation, https://markupsafe.palletsprojects.com/
37
+ Project-URL: Changes, https://markupsafe.palletsprojects.com/changes/
38
+ Project-URL: Source, https://github.com/pallets/markupsafe/
39
+ Project-URL: Chat, https://discord.gg/pallets
40
+ Classifier: Development Status :: 5 - Production/Stable
41
+ Classifier: Environment :: Web Environment
42
+ Classifier: Intended Audience :: Developers
43
+ Classifier: License :: OSI Approved :: BSD License
44
+ Classifier: Operating System :: OS Independent
45
+ Classifier: Programming Language :: Python
46
+ Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
47
+ Classifier: Topic :: Text Processing :: Markup :: HTML
48
+ Classifier: Typing :: Typed
49
+ Requires-Python: >=3.9
50
+ Description-Content-Type: text/markdown
51
+ License-File: LICENSE.txt
52
+
53
+ # MarkupSafe
54
+
55
+ MarkupSafe implements a text object that escapes characters so it is
56
+ safe to use in HTML and XML. Characters that have special meanings are
57
+ replaced so that they display as the actual characters. This mitigates
58
+ injection attacks, meaning untrusted user input can safely be displayed
59
+ on a page.
60
+
61
+
62
+ ## Examples
63
+
64
+ ```pycon
65
+ >>> from markupsafe import Markup, escape
66
+
67
+ >>> # escape replaces special characters and wraps in Markup
68
+ >>> escape("<script>alert(document.cookie);</script>")
69
+ Markup('&lt;script&gt;alert(document.cookie);&lt;/script&gt;')
70
+
71
+ >>> # wrap in Markup to mark text "safe" and prevent escaping
72
+ >>> Markup("<strong>Hello</strong>")
73
+ Markup('<strong>hello</strong>')
74
+
75
+ >>> escape(Markup("<strong>Hello</strong>"))
76
+ Markup('<strong>hello</strong>')
77
+
78
+ >>> # Markup is a str subclass
79
+ >>> # methods and operators escape their arguments
80
+ >>> template = Markup("Hello <em>{name}</em>")
81
+ >>> template.format(name='"World"')
82
+ Markup('Hello <em>&#34;World&#34;</em>')
83
+ ```
84
+
85
+ ## Donate
86
+
87
+ The Pallets organization develops and supports MarkupSafe and other
88
+ popular packages. In order to grow the community of contributors and
89
+ users, and allow the maintainers to devote more time to the projects,
90
+ [please donate today][].
91
+
92
+ [please donate today]: https://palletsprojects.com/donate
venv/lib/python3.10/site-packages/MarkupSafe-3.0.2.dist-info/RECORD ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MarkupSafe-3.0.2.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
2
+ MarkupSafe-3.0.2.dist-info/LICENSE.txt,sha256=SJqOEQhQntmKN7uYPhHg9-HTHwvY-Zp5yESOf_N9B-o,1475
3
+ MarkupSafe-3.0.2.dist-info/METADATA,sha256=aAwbZhSmXdfFuMM-rEHpeiHRkBOGESyVLJIuwzHP-nw,3975
4
+ MarkupSafe-3.0.2.dist-info/RECORD,,
5
+ MarkupSafe-3.0.2.dist-info/WHEEL,sha256=_kVlewavvOSnwZE_whBk3jlE_Ob-nL5GvlVcLkpXSD8,151
6
+ MarkupSafe-3.0.2.dist-info/top_level.txt,sha256=qy0Plje5IJuvsCBjejJyhDCjEAdcDLK_2agVcex8Z6U,11
7
+ markupsafe/__init__.py,sha256=sr-U6_27DfaSrj5jnHYxWN-pvhM27sjlDplMDPZKm7k,13214
8
+ markupsafe/__pycache__/__init__.cpython-310.pyc,,
9
+ markupsafe/__pycache__/_native.cpython-310.pyc,,
10
+ markupsafe/_native.py,sha256=hSLs8Jmz5aqayuengJJ3kdT5PwNpBWpKrmQSdipndC8,210
11
+ markupsafe/_speedups.c,sha256=O7XulmTo-epI6n2FtMVOrJXl8EAaIwD2iNYmBI5SEoQ,4149
12
+ markupsafe/_speedups.cpython-310-x86_64-linux-gnu.so,sha256=x4RoxWgyqAEokk-AZrWvrLDxLE-dm-zZSZYV_gOiLJA,34976
13
+ markupsafe/_speedups.pyi,sha256=ENd1bYe7gbBUf2ywyYWOGUpnXOHNJ-cgTNqetlW8h5k,41
14
+ markupsafe/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
venv/lib/python3.10/site-packages/MarkupSafe-3.0.2.dist-info/WHEEL ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.2.0)
3
+ Root-Is-Purelib: false
4
+ Tag: cp310-cp310-manylinux_2_17_x86_64
5
+ Tag: cp310-cp310-manylinux2014_x86_64
6
+
venv/lib/python3.10/site-packages/MarkupSafe-3.0.2.dist-info/top_level.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ markupsafe
venv/lib/python3.10/site-packages/absl_py-2.3.1.dist-info/INSTALLER ADDED
@@ -0,0 +1 @@
 
 
1
+ pip
venv/lib/python3.10/site-packages/absl_py-2.3.1.dist-info/METADATA ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Metadata-Version: 2.4
2
+ Name: absl-py
3
+ Version: 2.3.1
4
+ Summary: Abseil Python Common Libraries, see https://github.com/abseil/abseil-py.
5
+ Project-URL: Changelog, https://github.com/abseil/abseil-py/blob/main/CHANGELOG.md
6
+ Project-URL: Documentation, https://abseil.io/docs/python/
7
+ Project-URL: Issues, https://github.com/abseil/abseil-py/issues
8
+ Project-URL: Source, https://github.com/abseil/abseil-py
9
+ Author: The Abseil Authors
10
+ License-Expression: Apache-2.0
11
+ License-File: AUTHORS
12
+ License-File: LICENSE
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: Apache Software License
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.8
19
+ Classifier: Programming Language :: Python :: 3.9
20
+ Classifier: Programming Language :: Python :: 3.10
21
+ Classifier: Programming Language :: Python :: 3.11
22
+ Classifier: Programming Language :: Python :: 3.12
23
+ Classifier: Programming Language :: Python :: 3.13
24
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
25
+ Requires-Python: >=3.8
26
+ Description-Content-Type: text/markdown
27
+
28
+ [![Package version](https://img.shields.io/pypi/v/absl-py)](https://pypi.org/project/absl-py)
29
+ [![Supported Python versions](https://img.shields.io/pypi/pyversions/absl-py.svg?style=flat-square)](https://pypi.org/project/absl-py)
30
+ [![License](https://img.shields.io/github/license/abseil/abseil-py)](https://github.com/abseil/abseil-py/blob/main/LICENSE)
31
+ [![Build Status](https://github.com/abseil/abseil-py/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/abseil/abseil-py/actions)
32
+ [![Overall downloads](https://pepy.tech/badge/absl-py)](https://pepy.tech/project/absl-py)
33
+ [![Last month downloads](https://pepy.tech/badge/absl-py/month)](https://pepy.tech/project/absl-py)
34
+
35
+ # Abseil Python Common Libraries
36
+
37
+ This repository is a collection of Python library code for building Python
38
+ applications. The code is collected from Google's own Python code base, and has
39
+ been extensively tested and used in production.
40
+
41
+ ## Features
42
+
43
+ * Simple application startup
44
+ * Distributed commandline flags system
45
+ * Custom logging module with additional features
46
+ * Testing utilities
47
+
48
+ ## Getting Started
49
+
50
+ ### Installation
51
+
52
+ To install the package, simply run:
53
+
54
+ ```bash
55
+ pip install absl-py
56
+ ```
57
+
58
+ Or install from source:
59
+
60
+ ```bash
61
+ pip install .
62
+ ```
63
+
64
+ ### Running Tests
65
+
66
+ To run Abseil tests, you can clone the git repo and run
67
+ [bazel](https://bazel.build/):
68
+
69
+ ```bash
70
+ git clone https://github.com/abseil/abseil-py.git
71
+ cd abseil-py
72
+ bazel test absl/...
73
+ ```
74
+
75
+ Please also validate the type annotations against the latest mypy:
76
+
77
+ ```bash
78
+ pip install mypy
79
+ mypy absl
80
+ ```
81
+
82
+ ### Example Code
83
+
84
+ Please refer to
85
+ [smoke_tests/sample_app.py](https://github.com/abseil/abseil-py/blob/main/smoke_tests/sample_app.py)
86
+ as an example to get started.
87
+
88
+ ## Documentation
89
+
90
+ See the [Abseil Python Developer Guide](https://abseil.io/docs/python/).
91
+
92
+ ## Future Releases
93
+
94
+ The current repository includes an initial set of libraries for early adoption.
95
+ More components and interoperability with Abseil C++ Common Libraries
96
+ will come in future releases.
97
+
98
+ ## License
99
+
100
+ The Abseil Python library is licensed under the terms of the Apache
101
+ license. See [LICENSE](LICENSE) for more information.
venv/lib/python3.10/site-packages/absl_py-2.3.1.dist-info/RECORD ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ absl/__init__.py,sha256=9iJ5PwnoYh12ztceccjD2JbnSvx6d2JVElEurn9cqJk,607
2
+ absl/__pycache__/__init__.cpython-310.pyc,,
3
+ absl/__pycache__/app.cpython-310.pyc,,
4
+ absl/__pycache__/command_name.cpython-310.pyc,,
5
+ absl/app.py,sha256=0WJpjLaYcgcXtl9NTn-i5jSEfAoweMg4SXAel2XLF0Y,15360
6
+ absl/app.pyi,sha256=NdRh0OBqWHKnc3hEjdLaspLdu6aDHn3XP360ZPZeQE8,1885
7
+ absl/command_name.py,sha256=FgWUMHmlX0yQxEuMPXxFxn5ayWWZSLB0cq8Vx361TpU,2283
8
+ absl/flags/__init__.py,sha256=n_uLeSK-15_1DKLb0wXivmt6A8xvADDC87D1yoJdqVU,7665
9
+ absl/flags/__pycache__/__init__.cpython-310.pyc,,
10
+ absl/flags/__pycache__/_argument_parser.cpython-310.pyc,,
11
+ absl/flags/__pycache__/_defines.cpython-310.pyc,,
12
+ absl/flags/__pycache__/_exceptions.cpython-310.pyc,,
13
+ absl/flags/__pycache__/_flag.cpython-310.pyc,,
14
+ absl/flags/__pycache__/_flagvalues.cpython-310.pyc,,
15
+ absl/flags/__pycache__/_helpers.cpython-310.pyc,,
16
+ absl/flags/__pycache__/_validators.cpython-310.pyc,,
17
+ absl/flags/__pycache__/_validators_classes.cpython-310.pyc,,
18
+ absl/flags/__pycache__/argparse_flags.cpython-310.pyc,,
19
+ absl/flags/_argument_parser.py,sha256=arzkHA3CbPJdJ7diqUyskdoaLtKt8Aij1EZCVH_qJHU,20629
20
+ absl/flags/_defines.py,sha256=NAK489NGv3YMvbYj2Jzl6q89HmAtmMI65di1L_RK7XE,52895
21
+ absl/flags/_exceptions.py,sha256=FZzlzhvkjqPImTxXqbS1pSPYKr_TvtOd5ellvoiVLDI,3619
22
+ absl/flags/_flag.py,sha256=mdMJFklKQdCi9WsWPvUlmBRoQ2IAPW0Z0lDIKj0Lsx8,20079
23
+ absl/flags/_flagvalues.py,sha256=cGzMsWxthqGYfpESReRSJCXjpRujosSmTT-efJW3CbQ,54364
24
+ absl/flags/_helpers.py,sha256=MHbgtRkbNpVnrr7_NCdkFi_x3voQa-1-bypqsunHCJE,14154
25
+ absl/flags/_validators.py,sha256=VcsJtZzohliNxsI974NECYpeozD8rswHNHXggrQ4BLo,14140
26
+ absl/flags/_validators_classes.py,sha256=PGUWzO7v3wPOHb9leIKKzry3q-pPeKCoMB_O7prLdnY,6093
27
+ absl/flags/argparse_flags.py,sha256=usJudgMpy3P6Vvq7-LmJNa2Rj3ygHM3hwDTGd1mbAzc,14386
28
+ absl/logging/__init__.py,sha256=XpcmJrFEzDK2iWTlmDwtNSScSiGiNedFibWuW0tWMbk,43583
29
+ absl/logging/__init__.pyi,sha256=39EEBOH_rAyDQJpwyito2vo4IAZP9hnw3-wXC_Gulvc,5925
30
+ absl/logging/__pycache__/__init__.cpython-310.pyc,,
31
+ absl/logging/__pycache__/converter.cpython-310.pyc,,
32
+ absl/logging/converter.py,sha256=6eBymfv9UNkog0BGat4HPWlxC_oSqvHcQ46jnSdtaMg,6323
33
+ absl/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
34
+ absl/testing/__init__.py,sha256=7cM57swk2T1Hc5wxmt-JpcaR6xfdPJyL_lyRqgODvuM,584
35
+ absl/testing/__pycache__/__init__.cpython-310.pyc,,
36
+ absl/testing/__pycache__/_bazelize_command.cpython-310.pyc,,
37
+ absl/testing/__pycache__/_pretty_print_reporter.cpython-310.pyc,,
38
+ absl/testing/__pycache__/absltest.cpython-310.pyc,,
39
+ absl/testing/__pycache__/flagsaver.cpython-310.pyc,,
40
+ absl/testing/__pycache__/parameterized.cpython-310.pyc,,
41
+ absl/testing/__pycache__/xml_reporter.cpython-310.pyc,,
42
+ absl/testing/_bazelize_command.py,sha256=qpioV02ln2sBBJ9kdlHgNpKk8_wxdz2hJGKbG6EWZMI,2287
43
+ absl/testing/_pretty_print_reporter.py,sha256=PZh9NXSXBbXDi0FOk-BOmpse8LXa92Er16tgyBRogMs,3065
44
+ absl/testing/absltest.py,sha256=PW4c4SVlOUPSvFIhayimJY2LEbJ7BAojob6u9SBUrck,105675
45
+ absl/testing/flagsaver.py,sha256=HIWzFyayy-Pa8TqTAXF7BtHQ7s9Uk68KE8AVVAM0o0w,13346
46
+ absl/testing/parameterized.py,sha256=TpTlWTUXjikGeUDE45AgubvnmHsPbFNj-wUtAwT-e6E,27817
47
+ absl/testing/xml_reporter.py,sha256=YFkM7SROW8aeoCCn8IsGZ4cqXu4x8MwL1oN42-OtPKI,21430
48
+ absl_py-2.3.1.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
49
+ absl_py-2.3.1.dist-info/METADATA,sha256=2HZS24vkmHw7GQqUWcDE_FdVXjW1dgVS7pWPdWJ5Yvg,3331
50
+ absl_py-2.3.1.dist-info/RECORD,,
51
+ absl_py-2.3.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
52
+ absl_py-2.3.1.dist-info/licenses/AUTHORS,sha256=YoLudsylaQg7W5mLn4FroQMuEnuNx8RpQrhkd_xvv6U,296
53
+ absl_py-2.3.1.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
venv/lib/python3.10/site-packages/absl_py-2.3.1.dist-info/WHEEL ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
venv/lib/python3.10/site-packages/absl_py-2.3.1.dist-info/licenses/AUTHORS ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ # This is the list of Abseil authors for copyright purposes.
2
+ #
3
+ # This does not necessarily list everyone who has contributed code, since in
4
+ # some cases, their employer may be the copyright holder. To see the full list
5
+ # of contributors, see the revision history in source control.
6
+
7
+ Google Inc.
venv/lib/python3.10/site-packages/absl_py-2.3.1.dist-info/licenses/LICENSE ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
venv/lib/python3.10/site-packages/attr/__init__.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+
3
+ """
4
+ Classes Without Boilerplate
5
+ """
6
+
7
+ from functools import partial
8
+ from typing import Callable, Literal, Protocol
9
+
10
+ from . import converters, exceptions, filters, setters, validators
11
+ from ._cmp import cmp_using
12
+ from ._config import get_run_validators, set_run_validators
13
+ from ._funcs import asdict, assoc, astuple, has, resolve_types
14
+ from ._make import (
15
+ NOTHING,
16
+ Attribute,
17
+ Converter,
18
+ Factory,
19
+ _Nothing,
20
+ attrib,
21
+ attrs,
22
+ evolve,
23
+ fields,
24
+ fields_dict,
25
+ make_class,
26
+ validate,
27
+ )
28
+ from ._next_gen import define, field, frozen, mutable
29
+ from ._version_info import VersionInfo
30
+
31
+
32
+ s = attributes = attrs
33
+ ib = attr = attrib
34
+ dataclass = partial(attrs, auto_attribs=True) # happy Easter ;)
35
+
36
+
37
+ class AttrsInstance(Protocol):
38
+ pass
39
+
40
+
41
+ NothingType = Literal[_Nothing.NOTHING]
42
+
43
+ __all__ = [
44
+ "NOTHING",
45
+ "Attribute",
46
+ "AttrsInstance",
47
+ "Converter",
48
+ "Factory",
49
+ "NothingType",
50
+ "asdict",
51
+ "assoc",
52
+ "astuple",
53
+ "attr",
54
+ "attrib",
55
+ "attributes",
56
+ "attrs",
57
+ "cmp_using",
58
+ "converters",
59
+ "define",
60
+ "evolve",
61
+ "exceptions",
62
+ "field",
63
+ "fields",
64
+ "fields_dict",
65
+ "filters",
66
+ "frozen",
67
+ "get_run_validators",
68
+ "has",
69
+ "ib",
70
+ "make_class",
71
+ "mutable",
72
+ "resolve_types",
73
+ "s",
74
+ "set_run_validators",
75
+ "setters",
76
+ "validate",
77
+ "validators",
78
+ ]
79
+
80
+
81
+ def _make_getattr(mod_name: str) -> Callable:
82
+ """
83
+ Create a metadata proxy for packaging information that uses *mod_name* in
84
+ its warnings and errors.
85
+ """
86
+
87
+ def __getattr__(name: str) -> str:
88
+ if name not in ("__version__", "__version_info__"):
89
+ msg = f"module {mod_name} has no attribute {name}"
90
+ raise AttributeError(msg)
91
+
92
+ from importlib.metadata import metadata
93
+
94
+ meta = metadata("attrs")
95
+
96
+ if name == "__version_info__":
97
+ return VersionInfo._from_version_string(meta["version"])
98
+
99
+ return meta["version"]
100
+
101
+ return __getattr__
102
+
103
+
104
+ __getattr__ = _make_getattr(__name__)
venv/lib/python3.10/site-packages/attr/__init__.pyi ADDED
@@ -0,0 +1,389 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import enum
2
+ import sys
3
+
4
+ from typing import (
5
+ Any,
6
+ Callable,
7
+ Generic,
8
+ Literal,
9
+ Mapping,
10
+ Protocol,
11
+ Sequence,
12
+ TypeVar,
13
+ overload,
14
+ )
15
+
16
+ # `import X as X` is required to make these public
17
+ from . import converters as converters
18
+ from . import exceptions as exceptions
19
+ from . import filters as filters
20
+ from . import setters as setters
21
+ from . import validators as validators
22
+ from ._cmp import cmp_using as cmp_using
23
+ from ._typing_compat import AttrsInstance_
24
+ from ._version_info import VersionInfo
25
+ from attrs import (
26
+ define as define,
27
+ field as field,
28
+ mutable as mutable,
29
+ frozen as frozen,
30
+ _EqOrderType,
31
+ _ValidatorType,
32
+ _ConverterType,
33
+ _ReprArgType,
34
+ _OnSetAttrType,
35
+ _OnSetAttrArgType,
36
+ _FieldTransformer,
37
+ _ValidatorArgType,
38
+ )
39
+
40
+ if sys.version_info >= (3, 10):
41
+ from typing import TypeGuard, TypeAlias
42
+ else:
43
+ from typing_extensions import TypeGuard, TypeAlias
44
+
45
+ if sys.version_info >= (3, 11):
46
+ from typing import dataclass_transform
47
+ else:
48
+ from typing_extensions import dataclass_transform
49
+
50
+ __version__: str
51
+ __version_info__: VersionInfo
52
+ __title__: str
53
+ __description__: str
54
+ __url__: str
55
+ __uri__: str
56
+ __author__: str
57
+ __email__: str
58
+ __license__: str
59
+ __copyright__: str
60
+
61
+ _T = TypeVar("_T")
62
+ _C = TypeVar("_C", bound=type)
63
+
64
+ _FilterType = Callable[["Attribute[_T]", _T], bool]
65
+
66
+ # We subclass this here to keep the protocol's qualified name clean.
67
+ class AttrsInstance(AttrsInstance_, Protocol):
68
+ pass
69
+
70
+ _A = TypeVar("_A", bound=type[AttrsInstance])
71
+
72
+ class _Nothing(enum.Enum):
73
+ NOTHING = enum.auto()
74
+
75
+ NOTHING = _Nothing.NOTHING
76
+ NothingType: TypeAlias = Literal[_Nothing.NOTHING]
77
+
78
+ # NOTE: Factory lies about its return type to make this possible:
79
+ # `x: List[int] # = Factory(list)`
80
+ # Work around mypy issue #4554 in the common case by using an overload.
81
+
82
+ @overload
83
+ def Factory(factory: Callable[[], _T]) -> _T: ...
84
+ @overload
85
+ def Factory(
86
+ factory: Callable[[Any], _T],
87
+ takes_self: Literal[True],
88
+ ) -> _T: ...
89
+ @overload
90
+ def Factory(
91
+ factory: Callable[[], _T],
92
+ takes_self: Literal[False],
93
+ ) -> _T: ...
94
+
95
+ In = TypeVar("In")
96
+ Out = TypeVar("Out")
97
+
98
+ class Converter(Generic[In, Out]):
99
+ @overload
100
+ def __init__(self, converter: Callable[[In], Out]) -> None: ...
101
+ @overload
102
+ def __init__(
103
+ self,
104
+ converter: Callable[[In, AttrsInstance, Attribute], Out],
105
+ *,
106
+ takes_self: Literal[True],
107
+ takes_field: Literal[True],
108
+ ) -> None: ...
109
+ @overload
110
+ def __init__(
111
+ self,
112
+ converter: Callable[[In, Attribute], Out],
113
+ *,
114
+ takes_field: Literal[True],
115
+ ) -> None: ...
116
+ @overload
117
+ def __init__(
118
+ self,
119
+ converter: Callable[[In, AttrsInstance], Out],
120
+ *,
121
+ takes_self: Literal[True],
122
+ ) -> None: ...
123
+
124
+ class Attribute(Generic[_T]):
125
+ name: str
126
+ default: _T | None
127
+ validator: _ValidatorType[_T] | None
128
+ repr: _ReprArgType
129
+ cmp: _EqOrderType
130
+ eq: _EqOrderType
131
+ order: _EqOrderType
132
+ hash: bool | None
133
+ init: bool
134
+ converter: Converter | None
135
+ metadata: dict[Any, Any]
136
+ type: type[_T] | None
137
+ kw_only: bool
138
+ on_setattr: _OnSetAttrType
139
+ alias: str | None
140
+
141
+ def evolve(self, **changes: Any) -> "Attribute[Any]": ...
142
+
143
+ # NOTE: We had several choices for the annotation to use for type arg:
144
+ # 1) Type[_T]
145
+ # - Pros: Handles simple cases correctly
146
+ # - Cons: Might produce less informative errors in the case of conflicting
147
+ # TypeVars e.g. `attr.ib(default='bad', type=int)`
148
+ # 2) Callable[..., _T]
149
+ # - Pros: Better error messages than #1 for conflicting TypeVars
150
+ # - Cons: Terrible error messages for validator checks.
151
+ # e.g. attr.ib(type=int, validator=validate_str)
152
+ # -> error: Cannot infer function type argument
153
+ # 3) type (and do all of the work in the mypy plugin)
154
+ # - Pros: Simple here, and we could customize the plugin with our own errors.
155
+ # - Cons: Would need to write mypy plugin code to handle all the cases.
156
+ # We chose option #1.
157
+
158
+ # `attr` lies about its return type to make the following possible:
159
+ # attr() -> Any
160
+ # attr(8) -> int
161
+ # attr(validator=<some callable>) -> Whatever the callable expects.
162
+ # This makes this type of assignments possible:
163
+ # x: int = attr(8)
164
+ #
165
+ # This form catches explicit None or no default but with no other arguments
166
+ # returns Any.
167
+ @overload
168
+ def attrib(
169
+ default: None = ...,
170
+ validator: None = ...,
171
+ repr: _ReprArgType = ...,
172
+ cmp: _EqOrderType | None = ...,
173
+ hash: bool | None = ...,
174
+ init: bool = ...,
175
+ metadata: Mapping[Any, Any] | None = ...,
176
+ type: None = ...,
177
+ converter: None = ...,
178
+ factory: None = ...,
179
+ kw_only: bool = ...,
180
+ eq: _EqOrderType | None = ...,
181
+ order: _EqOrderType | None = ...,
182
+ on_setattr: _OnSetAttrArgType | None = ...,
183
+ alias: str | None = ...,
184
+ ) -> Any: ...
185
+
186
+ # This form catches an explicit None or no default and infers the type from the
187
+ # other arguments.
188
+ @overload
189
+ def attrib(
190
+ default: None = ...,
191
+ validator: _ValidatorArgType[_T] | None = ...,
192
+ repr: _ReprArgType = ...,
193
+ cmp: _EqOrderType | None = ...,
194
+ hash: bool | None = ...,
195
+ init: bool = ...,
196
+ metadata: Mapping[Any, Any] | None = ...,
197
+ type: type[_T] | None = ...,
198
+ converter: _ConverterType
199
+ | list[_ConverterType]
200
+ | tuple[_ConverterType]
201
+ | None = ...,
202
+ factory: Callable[[], _T] | None = ...,
203
+ kw_only: bool = ...,
204
+ eq: _EqOrderType | None = ...,
205
+ order: _EqOrderType | None = ...,
206
+ on_setattr: _OnSetAttrArgType | None = ...,
207
+ alias: str | None = ...,
208
+ ) -> _T: ...
209
+
210
+ # This form catches an explicit default argument.
211
+ @overload
212
+ def attrib(
213
+ default: _T,
214
+ validator: _ValidatorArgType[_T] | None = ...,
215
+ repr: _ReprArgType = ...,
216
+ cmp: _EqOrderType | None = ...,
217
+ hash: bool | None = ...,
218
+ init: bool = ...,
219
+ metadata: Mapping[Any, Any] | None = ...,
220
+ type: type[_T] | None = ...,
221
+ converter: _ConverterType
222
+ | list[_ConverterType]
223
+ | tuple[_ConverterType]
224
+ | None = ...,
225
+ factory: Callable[[], _T] | None = ...,
226
+ kw_only: bool = ...,
227
+ eq: _EqOrderType | None = ...,
228
+ order: _EqOrderType | None = ...,
229
+ on_setattr: _OnSetAttrArgType | None = ...,
230
+ alias: str | None = ...,
231
+ ) -> _T: ...
232
+
233
+ # This form covers type=non-Type: e.g. forward references (str), Any
234
+ @overload
235
+ def attrib(
236
+ default: _T | None = ...,
237
+ validator: _ValidatorArgType[_T] | None = ...,
238
+ repr: _ReprArgType = ...,
239
+ cmp: _EqOrderType | None = ...,
240
+ hash: bool | None = ...,
241
+ init: bool = ...,
242
+ metadata: Mapping[Any, Any] | None = ...,
243
+ type: object = ...,
244
+ converter: _ConverterType
245
+ | list[_ConverterType]
246
+ | tuple[_ConverterType]
247
+ | None = ...,
248
+ factory: Callable[[], _T] | None = ...,
249
+ kw_only: bool = ...,
250
+ eq: _EqOrderType | None = ...,
251
+ order: _EqOrderType | None = ...,
252
+ on_setattr: _OnSetAttrArgType | None = ...,
253
+ alias: str | None = ...,
254
+ ) -> Any: ...
255
+ @overload
256
+ @dataclass_transform(order_default=True, field_specifiers=(attrib, field))
257
+ def attrs(
258
+ maybe_cls: _C,
259
+ these: dict[str, Any] | None = ...,
260
+ repr_ns: str | None = ...,
261
+ repr: bool = ...,
262
+ cmp: _EqOrderType | None = ...,
263
+ hash: bool | None = ...,
264
+ init: bool = ...,
265
+ slots: bool = ...,
266
+ frozen: bool = ...,
267
+ weakref_slot: bool = ...,
268
+ str: bool = ...,
269
+ auto_attribs: bool = ...,
270
+ kw_only: bool = ...,
271
+ cache_hash: bool = ...,
272
+ auto_exc: bool = ...,
273
+ eq: _EqOrderType | None = ...,
274
+ order: _EqOrderType | None = ...,
275
+ auto_detect: bool = ...,
276
+ collect_by_mro: bool = ...,
277
+ getstate_setstate: bool | None = ...,
278
+ on_setattr: _OnSetAttrArgType | None = ...,
279
+ field_transformer: _FieldTransformer | None = ...,
280
+ match_args: bool = ...,
281
+ unsafe_hash: bool | None = ...,
282
+ ) -> _C: ...
283
+ @overload
284
+ @dataclass_transform(order_default=True, field_specifiers=(attrib, field))
285
+ def attrs(
286
+ maybe_cls: None = ...,
287
+ these: dict[str, Any] | None = ...,
288
+ repr_ns: str | None = ...,
289
+ repr: bool = ...,
290
+ cmp: _EqOrderType | None = ...,
291
+ hash: bool | None = ...,
292
+ init: bool = ...,
293
+ slots: bool = ...,
294
+ frozen: bool = ...,
295
+ weakref_slot: bool = ...,
296
+ str: bool = ...,
297
+ auto_attribs: bool = ...,
298
+ kw_only: bool = ...,
299
+ cache_hash: bool = ...,
300
+ auto_exc: bool = ...,
301
+ eq: _EqOrderType | None = ...,
302
+ order: _EqOrderType | None = ...,
303
+ auto_detect: bool = ...,
304
+ collect_by_mro: bool = ...,
305
+ getstate_setstate: bool | None = ...,
306
+ on_setattr: _OnSetAttrArgType | None = ...,
307
+ field_transformer: _FieldTransformer | None = ...,
308
+ match_args: bool = ...,
309
+ unsafe_hash: bool | None = ...,
310
+ ) -> Callable[[_C], _C]: ...
311
+ def fields(cls: type[AttrsInstance]) -> Any: ...
312
+ def fields_dict(cls: type[AttrsInstance]) -> dict[str, Attribute[Any]]: ...
313
+ def validate(inst: AttrsInstance) -> None: ...
314
+ def resolve_types(
315
+ cls: _A,
316
+ globalns: dict[str, Any] | None = ...,
317
+ localns: dict[str, Any] | None = ...,
318
+ attribs: list[Attribute[Any]] | None = ...,
319
+ include_extras: bool = ...,
320
+ ) -> _A: ...
321
+
322
+ # TODO: add support for returning a proper attrs class from the mypy plugin
323
+ # we use Any instead of _CountingAttr so that e.g. `make_class('Foo',
324
+ # [attr.ib()])` is valid
325
+ def make_class(
326
+ name: str,
327
+ attrs: list[str] | tuple[str, ...] | dict[str, Any],
328
+ bases: tuple[type, ...] = ...,
329
+ class_body: dict[str, Any] | None = ...,
330
+ repr_ns: str | None = ...,
331
+ repr: bool = ...,
332
+ cmp: _EqOrderType | None = ...,
333
+ hash: bool | None = ...,
334
+ init: bool = ...,
335
+ slots: bool = ...,
336
+ frozen: bool = ...,
337
+ weakref_slot: bool = ...,
338
+ str: bool = ...,
339
+ auto_attribs: bool = ...,
340
+ kw_only: bool = ...,
341
+ cache_hash: bool = ...,
342
+ auto_exc: bool = ...,
343
+ eq: _EqOrderType | None = ...,
344
+ order: _EqOrderType | None = ...,
345
+ collect_by_mro: bool = ...,
346
+ on_setattr: _OnSetAttrArgType | None = ...,
347
+ field_transformer: _FieldTransformer | None = ...,
348
+ ) -> type: ...
349
+
350
+ # _funcs --
351
+
352
+ # TODO: add support for returning TypedDict from the mypy plugin
353
+ # FIXME: asdict/astuple do not honor their factory args. Waiting on one of
354
+ # these:
355
+ # https://github.com/python/mypy/issues/4236
356
+ # https://github.com/python/typing/issues/253
357
+ # XXX: remember to fix attrs.asdict/astuple too!
358
+ def asdict(
359
+ inst: AttrsInstance,
360
+ recurse: bool = ...,
361
+ filter: _FilterType[Any] | None = ...,
362
+ dict_factory: type[Mapping[Any, Any]] = ...,
363
+ retain_collection_types: bool = ...,
364
+ value_serializer: Callable[[type, Attribute[Any], Any], Any] | None = ...,
365
+ tuple_keys: bool | None = ...,
366
+ ) -> dict[str, Any]: ...
367
+
368
+ # TODO: add support for returning NamedTuple from the mypy plugin
369
+ def astuple(
370
+ inst: AttrsInstance,
371
+ recurse: bool = ...,
372
+ filter: _FilterType[Any] | None = ...,
373
+ tuple_factory: type[Sequence[Any]] = ...,
374
+ retain_collection_types: bool = ...,
375
+ ) -> tuple[Any, ...]: ...
376
+ def has(cls: type) -> TypeGuard[type[AttrsInstance]]: ...
377
+ def assoc(inst: _T, **changes: Any) -> _T: ...
378
+ def evolve(inst: _T, **changes: Any) -> _T: ...
379
+
380
+ # _config --
381
+
382
+ def set_run_validators(run: bool) -> None: ...
383
+ def get_run_validators() -> bool: ...
384
+
385
+ # aliases --
386
+
387
+ s = attributes = attrs
388
+ ib = attr = attrib
389
+ dataclass = attrs # Technically, partial(attrs, auto_attribs=True) ;)
venv/lib/python3.10/site-packages/attr/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (2.31 kB). View file
 
venv/lib/python3.10/site-packages/attr/__pycache__/_cmp.cpython-310.pyc ADDED
Binary file (4.09 kB). View file
 
venv/lib/python3.10/site-packages/attr/__pycache__/_compat.cpython-310.pyc ADDED
Binary file (2.49 kB). View file
 
venv/lib/python3.10/site-packages/attr/__pycache__/_config.cpython-310.pyc ADDED
Binary file (1.07 kB). View file
 
venv/lib/python3.10/site-packages/attr/__pycache__/_funcs.cpython-310.pyc ADDED
Binary file (10.7 kB). View file
 
venv/lib/python3.10/site-packages/attr/__pycache__/_make.cpython-310.pyc ADDED
Binary file (69.6 kB). View file
 
venv/lib/python3.10/site-packages/attr/__pycache__/_next_gen.cpython-310.pyc ADDED
Binary file (23.7 kB). View file
 
venv/lib/python3.10/site-packages/attr/__pycache__/_version_info.cpython-310.pyc ADDED
Binary file (2.39 kB). View file
 
venv/lib/python3.10/site-packages/attr/__pycache__/converters.cpython-310.pyc ADDED
Binary file (3.8 kB). View file
 
venv/lib/python3.10/site-packages/attr/__pycache__/exceptions.cpython-310.pyc ADDED
Binary file (3.23 kB). View file
 
venv/lib/python3.10/site-packages/attr/__pycache__/filters.cpython-310.pyc ADDED
Binary file (2.32 kB). View file
 
venv/lib/python3.10/site-packages/attr/__pycache__/setters.cpython-310.pyc ADDED
Binary file (1.66 kB). View file
 
venv/lib/python3.10/site-packages/attr/__pycache__/validators.cpython-310.pyc ADDED
Binary file (20.4 kB). View file
 
venv/lib/python3.10/site-packages/attr/_cmp.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+
3
+
4
+ import functools
5
+ import types
6
+
7
+ from ._make import __ne__
8
+
9
+
10
+ _operation_names = {"eq": "==", "lt": "<", "le": "<=", "gt": ">", "ge": ">="}
11
+
12
+
13
+ def cmp_using(
14
+ eq=None,
15
+ lt=None,
16
+ le=None,
17
+ gt=None,
18
+ ge=None,
19
+ require_same_type=True,
20
+ class_name="Comparable",
21
+ ):
22
+ """
23
+ Create a class that can be passed into `attrs.field`'s ``eq``, ``order``,
24
+ and ``cmp`` arguments to customize field comparison.
25
+
26
+ The resulting class will have a full set of ordering methods if at least
27
+ one of ``{lt, le, gt, ge}`` and ``eq`` are provided.
28
+
29
+ Args:
30
+ eq (typing.Callable | None):
31
+ Callable used to evaluate equality of two objects.
32
+
33
+ lt (typing.Callable | None):
34
+ Callable used to evaluate whether one object is less than another
35
+ object.
36
+
37
+ le (typing.Callable | None):
38
+ Callable used to evaluate whether one object is less than or equal
39
+ to another object.
40
+
41
+ gt (typing.Callable | None):
42
+ Callable used to evaluate whether one object is greater than
43
+ another object.
44
+
45
+ ge (typing.Callable | None):
46
+ Callable used to evaluate whether one object is greater than or
47
+ equal to another object.
48
+
49
+ require_same_type (bool):
50
+ When `True`, equality and ordering methods will return
51
+ `NotImplemented` if objects are not of the same type.
52
+
53
+ class_name (str | None): Name of class. Defaults to "Comparable".
54
+
55
+ See `comparison` for more details.
56
+
57
+ .. versionadded:: 21.1.0
58
+ """
59
+
60
+ body = {
61
+ "__slots__": ["value"],
62
+ "__init__": _make_init(),
63
+ "_requirements": [],
64
+ "_is_comparable_to": _is_comparable_to,
65
+ }
66
+
67
+ # Add operations.
68
+ num_order_functions = 0
69
+ has_eq_function = False
70
+
71
+ if eq is not None:
72
+ has_eq_function = True
73
+ body["__eq__"] = _make_operator("eq", eq)
74
+ body["__ne__"] = __ne__
75
+
76
+ if lt is not None:
77
+ num_order_functions += 1
78
+ body["__lt__"] = _make_operator("lt", lt)
79
+
80
+ if le is not None:
81
+ num_order_functions += 1
82
+ body["__le__"] = _make_operator("le", le)
83
+
84
+ if gt is not None:
85
+ num_order_functions += 1
86
+ body["__gt__"] = _make_operator("gt", gt)
87
+
88
+ if ge is not None:
89
+ num_order_functions += 1
90
+ body["__ge__"] = _make_operator("ge", ge)
91
+
92
+ type_ = types.new_class(
93
+ class_name, (object,), {}, lambda ns: ns.update(body)
94
+ )
95
+
96
+ # Add same type requirement.
97
+ if require_same_type:
98
+ type_._requirements.append(_check_same_type)
99
+
100
+ # Add total ordering if at least one operation was defined.
101
+ if 0 < num_order_functions < 4:
102
+ if not has_eq_function:
103
+ # functools.total_ordering requires __eq__ to be defined,
104
+ # so raise early error here to keep a nice stack.
105
+ msg = "eq must be define is order to complete ordering from lt, le, gt, ge."
106
+ raise ValueError(msg)
107
+ type_ = functools.total_ordering(type_)
108
+
109
+ return type_
110
+
111
+
112
+ def _make_init():
113
+ """
114
+ Create __init__ method.
115
+ """
116
+
117
+ def __init__(self, value):
118
+ """
119
+ Initialize object with *value*.
120
+ """
121
+ self.value = value
122
+
123
+ return __init__
124
+
125
+
126
+ def _make_operator(name, func):
127
+ """
128
+ Create operator method.
129
+ """
130
+
131
+ def method(self, other):
132
+ if not self._is_comparable_to(other):
133
+ return NotImplemented
134
+
135
+ result = func(self.value, other.value)
136
+ if result is NotImplemented:
137
+ return NotImplemented
138
+
139
+ return result
140
+
141
+ method.__name__ = f"__{name}__"
142
+ method.__doc__ = (
143
+ f"Return a {_operation_names[name]} b. Computed by attrs."
144
+ )
145
+
146
+ return method
147
+
148
+
149
+ def _is_comparable_to(self, other):
150
+ """
151
+ Check whether `other` is comparable to `self`.
152
+ """
153
+ return all(func(self, other) for func in self._requirements)
154
+
155
+
156
+ def _check_same_type(self, other):
157
+ """
158
+ Return True if *self* and *other* are of the same type, False otherwise.
159
+ """
160
+ return other.value.__class__ is self.value.__class__
venv/lib/python3.10/site-packages/attr/_cmp.pyi ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Callable
2
+
3
+ _CompareWithType = Callable[[Any, Any], bool]
4
+
5
+ def cmp_using(
6
+ eq: _CompareWithType | None = ...,
7
+ lt: _CompareWithType | None = ...,
8
+ le: _CompareWithType | None = ...,
9
+ gt: _CompareWithType | None = ...,
10
+ ge: _CompareWithType | None = ...,
11
+ require_same_type: bool = ...,
12
+ class_name: str = ...,
13
+ ) -> type: ...
venv/lib/python3.10/site-packages/attr/_compat.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+
3
+ import inspect
4
+ import platform
5
+ import sys
6
+ import threading
7
+
8
+ from collections.abc import Mapping, Sequence # noqa: F401
9
+ from typing import _GenericAlias
10
+
11
+
12
+ PYPY = platform.python_implementation() == "PyPy"
13
+ PY_3_9_PLUS = sys.version_info[:2] >= (3, 9)
14
+ PY_3_10_PLUS = sys.version_info[:2] >= (3, 10)
15
+ PY_3_11_PLUS = sys.version_info[:2] >= (3, 11)
16
+ PY_3_12_PLUS = sys.version_info[:2] >= (3, 12)
17
+ PY_3_13_PLUS = sys.version_info[:2] >= (3, 13)
18
+ PY_3_14_PLUS = sys.version_info[:2] >= (3, 14)
19
+
20
+
21
+ if PY_3_14_PLUS: # pragma: no cover
22
+ import annotationlib
23
+
24
+ _get_annotations = annotationlib.get_annotations
25
+
26
+ else:
27
+
28
+ def _get_annotations(cls):
29
+ """
30
+ Get annotations for *cls*.
31
+ """
32
+ return cls.__dict__.get("__annotations__", {})
33
+
34
+
35
+ class _AnnotationExtractor:
36
+ """
37
+ Extract type annotations from a callable, returning None whenever there
38
+ is none.
39
+ """
40
+
41
+ __slots__ = ["sig"]
42
+
43
+ def __init__(self, callable):
44
+ try:
45
+ self.sig = inspect.signature(callable)
46
+ except (ValueError, TypeError): # inspect failed
47
+ self.sig = None
48
+
49
+ def get_first_param_type(self):
50
+ """
51
+ Return the type annotation of the first argument if it's not empty.
52
+ """
53
+ if not self.sig:
54
+ return None
55
+
56
+ params = list(self.sig.parameters.values())
57
+ if params and params[0].annotation is not inspect.Parameter.empty:
58
+ return params[0].annotation
59
+
60
+ return None
61
+
62
+ def get_return_type(self):
63
+ """
64
+ Return the return type if it's not empty.
65
+ """
66
+ if (
67
+ self.sig
68
+ and self.sig.return_annotation is not inspect.Signature.empty
69
+ ):
70
+ return self.sig.return_annotation
71
+
72
+ return None
73
+
74
+
75
+ # Thread-local global to track attrs instances which are already being repr'd.
76
+ # This is needed because there is no other (thread-safe) way to pass info
77
+ # about the instances that are already being repr'd through the call stack
78
+ # in order to ensure we don't perform infinite recursion.
79
+ #
80
+ # For instance, if an instance contains a dict which contains that instance,
81
+ # we need to know that we're already repr'ing the outside instance from within
82
+ # the dict's repr() call.
83
+ #
84
+ # This lives here rather than in _make.py so that the functions in _make.py
85
+ # don't have a direct reference to the thread-local in their globals dict.
86
+ # If they have such a reference, it breaks cloudpickle.
87
+ repr_context = threading.local()
88
+
89
+
90
+ def get_generic_base(cl):
91
+ """If this is a generic class (A[str]), return the generic base for it."""
92
+ if cl.__class__ is _GenericAlias:
93
+ return cl.__origin__
94
+ return None
venv/lib/python3.10/site-packages/attr/_config.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+
3
+ __all__ = ["get_run_validators", "set_run_validators"]
4
+
5
+ _run_validators = True
6
+
7
+
8
+ def set_run_validators(run):
9
+ """
10
+ Set whether or not validators are run. By default, they are run.
11
+
12
+ .. deprecated:: 21.3.0 It will not be removed, but it also will not be
13
+ moved to new ``attrs`` namespace. Use `attrs.validators.set_disabled()`
14
+ instead.
15
+ """
16
+ if not isinstance(run, bool):
17
+ msg = "'run' must be bool."
18
+ raise TypeError(msg)
19
+ global _run_validators
20
+ _run_validators = run
21
+
22
+
23
+ def get_run_validators():
24
+ """
25
+ Return whether or not validators are run.
26
+
27
+ .. deprecated:: 21.3.0 It will not be removed, but it also will not be
28
+ moved to new ``attrs`` namespace. Use `attrs.validators.get_disabled()`
29
+ instead.
30
+ """
31
+ return _run_validators
venv/lib/python3.10/site-packages/attr/_funcs.py ADDED
@@ -0,0 +1,468 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+
3
+
4
+ import copy
5
+
6
+ from ._compat import PY_3_9_PLUS, get_generic_base
7
+ from ._make import _OBJ_SETATTR, NOTHING, fields
8
+ from .exceptions import AttrsAttributeNotFoundError
9
+
10
+
11
+ def asdict(
12
+ inst,
13
+ recurse=True,
14
+ filter=None,
15
+ dict_factory=dict,
16
+ retain_collection_types=False,
17
+ value_serializer=None,
18
+ ):
19
+ """
20
+ Return the *attrs* attribute values of *inst* as a dict.
21
+
22
+ Optionally recurse into other *attrs*-decorated classes.
23
+
24
+ Args:
25
+ inst: Instance of an *attrs*-decorated class.
26
+
27
+ recurse (bool): Recurse into classes that are also *attrs*-decorated.
28
+
29
+ filter (~typing.Callable):
30
+ A callable whose return code determines whether an attribute or
31
+ element is included (`True`) or dropped (`False`). Is called with
32
+ the `attrs.Attribute` as the first argument and the value as the
33
+ second argument.
34
+
35
+ dict_factory (~typing.Callable):
36
+ A callable to produce dictionaries from. For example, to produce
37
+ ordered dictionaries instead of normal Python dictionaries, pass in
38
+ ``collections.OrderedDict``.
39
+
40
+ retain_collection_types (bool):
41
+ Do not convert to `list` when encountering an attribute whose type
42
+ is `tuple` or `set`. Only meaningful if *recurse* is `True`.
43
+
44
+ value_serializer (typing.Callable | None):
45
+ A hook that is called for every attribute or dict key/value. It
46
+ receives the current instance, field and value and must return the
47
+ (updated) value. The hook is run *after* the optional *filter* has
48
+ been applied.
49
+
50
+ Returns:
51
+ Return type of *dict_factory*.
52
+
53
+ Raises:
54
+ attrs.exceptions.NotAnAttrsClassError:
55
+ If *cls* is not an *attrs* class.
56
+
57
+ .. versionadded:: 16.0.0 *dict_factory*
58
+ .. versionadded:: 16.1.0 *retain_collection_types*
59
+ .. versionadded:: 20.3.0 *value_serializer*
60
+ .. versionadded:: 21.3.0
61
+ If a dict has a collection for a key, it is serialized as a tuple.
62
+ """
63
+ attrs = fields(inst.__class__)
64
+ rv = dict_factory()
65
+ for a in attrs:
66
+ v = getattr(inst, a.name)
67
+ if filter is not None and not filter(a, v):
68
+ continue
69
+
70
+ if value_serializer is not None:
71
+ v = value_serializer(inst, a, v)
72
+
73
+ if recurse is True:
74
+ if has(v.__class__):
75
+ rv[a.name] = asdict(
76
+ v,
77
+ recurse=True,
78
+ filter=filter,
79
+ dict_factory=dict_factory,
80
+ retain_collection_types=retain_collection_types,
81
+ value_serializer=value_serializer,
82
+ )
83
+ elif isinstance(v, (tuple, list, set, frozenset)):
84
+ cf = v.__class__ if retain_collection_types is True else list
85
+ items = [
86
+ _asdict_anything(
87
+ i,
88
+ is_key=False,
89
+ filter=filter,
90
+ dict_factory=dict_factory,
91
+ retain_collection_types=retain_collection_types,
92
+ value_serializer=value_serializer,
93
+ )
94
+ for i in v
95
+ ]
96
+ try:
97
+ rv[a.name] = cf(items)
98
+ except TypeError:
99
+ if not issubclass(cf, tuple):
100
+ raise
101
+ # Workaround for TypeError: cf.__new__() missing 1 required
102
+ # positional argument (which appears, for a namedturle)
103
+ rv[a.name] = cf(*items)
104
+ elif isinstance(v, dict):
105
+ df = dict_factory
106
+ rv[a.name] = df(
107
+ (
108
+ _asdict_anything(
109
+ kk,
110
+ is_key=True,
111
+ filter=filter,
112
+ dict_factory=df,
113
+ retain_collection_types=retain_collection_types,
114
+ value_serializer=value_serializer,
115
+ ),
116
+ _asdict_anything(
117
+ vv,
118
+ is_key=False,
119
+ filter=filter,
120
+ dict_factory=df,
121
+ retain_collection_types=retain_collection_types,
122
+ value_serializer=value_serializer,
123
+ ),
124
+ )
125
+ for kk, vv in v.items()
126
+ )
127
+ else:
128
+ rv[a.name] = v
129
+ else:
130
+ rv[a.name] = v
131
+ return rv
132
+
133
+
134
+ def _asdict_anything(
135
+ val,
136
+ is_key,
137
+ filter,
138
+ dict_factory,
139
+ retain_collection_types,
140
+ value_serializer,
141
+ ):
142
+ """
143
+ ``asdict`` only works on attrs instances, this works on anything.
144
+ """
145
+ if getattr(val.__class__, "__attrs_attrs__", None) is not None:
146
+ # Attrs class.
147
+ rv = asdict(
148
+ val,
149
+ recurse=True,
150
+ filter=filter,
151
+ dict_factory=dict_factory,
152
+ retain_collection_types=retain_collection_types,
153
+ value_serializer=value_serializer,
154
+ )
155
+ elif isinstance(val, (tuple, list, set, frozenset)):
156
+ if retain_collection_types is True:
157
+ cf = val.__class__
158
+ elif is_key:
159
+ cf = tuple
160
+ else:
161
+ cf = list
162
+
163
+ rv = cf(
164
+ [
165
+ _asdict_anything(
166
+ i,
167
+ is_key=False,
168
+ filter=filter,
169
+ dict_factory=dict_factory,
170
+ retain_collection_types=retain_collection_types,
171
+ value_serializer=value_serializer,
172
+ )
173
+ for i in val
174
+ ]
175
+ )
176
+ elif isinstance(val, dict):
177
+ df = dict_factory
178
+ rv = df(
179
+ (
180
+ _asdict_anything(
181
+ kk,
182
+ is_key=True,
183
+ filter=filter,
184
+ dict_factory=df,
185
+ retain_collection_types=retain_collection_types,
186
+ value_serializer=value_serializer,
187
+ ),
188
+ _asdict_anything(
189
+ vv,
190
+ is_key=False,
191
+ filter=filter,
192
+ dict_factory=df,
193
+ retain_collection_types=retain_collection_types,
194
+ value_serializer=value_serializer,
195
+ ),
196
+ )
197
+ for kk, vv in val.items()
198
+ )
199
+ else:
200
+ rv = val
201
+ if value_serializer is not None:
202
+ rv = value_serializer(None, None, rv)
203
+
204
+ return rv
205
+
206
+
207
+ def astuple(
208
+ inst,
209
+ recurse=True,
210
+ filter=None,
211
+ tuple_factory=tuple,
212
+ retain_collection_types=False,
213
+ ):
214
+ """
215
+ Return the *attrs* attribute values of *inst* as a tuple.
216
+
217
+ Optionally recurse into other *attrs*-decorated classes.
218
+
219
+ Args:
220
+ inst: Instance of an *attrs*-decorated class.
221
+
222
+ recurse (bool):
223
+ Recurse into classes that are also *attrs*-decorated.
224
+
225
+ filter (~typing.Callable):
226
+ A callable whose return code determines whether an attribute or
227
+ element is included (`True`) or dropped (`False`). Is called with
228
+ the `attrs.Attribute` as the first argument and the value as the
229
+ second argument.
230
+
231
+ tuple_factory (~typing.Callable):
232
+ A callable to produce tuples from. For example, to produce lists
233
+ instead of tuples.
234
+
235
+ retain_collection_types (bool):
236
+ Do not convert to `list` or `dict` when encountering an attribute
237
+ which type is `tuple`, `dict` or `set`. Only meaningful if
238
+ *recurse* is `True`.
239
+
240
+ Returns:
241
+ Return type of *tuple_factory*
242
+
243
+ Raises:
244
+ attrs.exceptions.NotAnAttrsClassError:
245
+ If *cls* is not an *attrs* class.
246
+
247
+ .. versionadded:: 16.2.0
248
+ """
249
+ attrs = fields(inst.__class__)
250
+ rv = []
251
+ retain = retain_collection_types # Very long. :/
252
+ for a in attrs:
253
+ v = getattr(inst, a.name)
254
+ if filter is not None and not filter(a, v):
255
+ continue
256
+ if recurse is True:
257
+ if has(v.__class__):
258
+ rv.append(
259
+ astuple(
260
+ v,
261
+ recurse=True,
262
+ filter=filter,
263
+ tuple_factory=tuple_factory,
264
+ retain_collection_types=retain,
265
+ )
266
+ )
267
+ elif isinstance(v, (tuple, list, set, frozenset)):
268
+ cf = v.__class__ if retain is True else list
269
+ items = [
270
+ (
271
+ astuple(
272
+ j,
273
+ recurse=True,
274
+ filter=filter,
275
+ tuple_factory=tuple_factory,
276
+ retain_collection_types=retain,
277
+ )
278
+ if has(j.__class__)
279
+ else j
280
+ )
281
+ for j in v
282
+ ]
283
+ try:
284
+ rv.append(cf(items))
285
+ except TypeError:
286
+ if not issubclass(cf, tuple):
287
+ raise
288
+ # Workaround for TypeError: cf.__new__() missing 1 required
289
+ # positional argument (which appears, for a namedturle)
290
+ rv.append(cf(*items))
291
+ elif isinstance(v, dict):
292
+ df = v.__class__ if retain is True else dict
293
+ rv.append(
294
+ df(
295
+ (
296
+ (
297
+ astuple(
298
+ kk,
299
+ tuple_factory=tuple_factory,
300
+ retain_collection_types=retain,
301
+ )
302
+ if has(kk.__class__)
303
+ else kk
304
+ ),
305
+ (
306
+ astuple(
307
+ vv,
308
+ tuple_factory=tuple_factory,
309
+ retain_collection_types=retain,
310
+ )
311
+ if has(vv.__class__)
312
+ else vv
313
+ ),
314
+ )
315
+ for kk, vv in v.items()
316
+ )
317
+ )
318
+ else:
319
+ rv.append(v)
320
+ else:
321
+ rv.append(v)
322
+
323
+ return rv if tuple_factory is list else tuple_factory(rv)
324
+
325
+
326
+ def has(cls):
327
+ """
328
+ Check whether *cls* is a class with *attrs* attributes.
329
+
330
+ Args:
331
+ cls (type): Class to introspect.
332
+
333
+ Raises:
334
+ TypeError: If *cls* is not a class.
335
+
336
+ Returns:
337
+ bool:
338
+ """
339
+ attrs = getattr(cls, "__attrs_attrs__", None)
340
+ if attrs is not None:
341
+ return True
342
+
343
+ # No attrs, maybe it's a specialized generic (A[str])?
344
+ generic_base = get_generic_base(cls)
345
+ if generic_base is not None:
346
+ generic_attrs = getattr(generic_base, "__attrs_attrs__", None)
347
+ if generic_attrs is not None:
348
+ # Stick it on here for speed next time.
349
+ cls.__attrs_attrs__ = generic_attrs
350
+ return generic_attrs is not None
351
+ return False
352
+
353
+
354
+ def assoc(inst, **changes):
355
+ """
356
+ Copy *inst* and apply *changes*.
357
+
358
+ This is different from `evolve` that applies the changes to the arguments
359
+ that create the new instance.
360
+
361
+ `evolve`'s behavior is preferable, but there are `edge cases`_ where it
362
+ doesn't work. Therefore `assoc` is deprecated, but will not be removed.
363
+
364
+ .. _`edge cases`: https://github.com/python-attrs/attrs/issues/251
365
+
366
+ Args:
367
+ inst: Instance of a class with *attrs* attributes.
368
+
369
+ changes: Keyword changes in the new copy.
370
+
371
+ Returns:
372
+ A copy of inst with *changes* incorporated.
373
+
374
+ Raises:
375
+ attrs.exceptions.AttrsAttributeNotFoundError:
376
+ If *attr_name* couldn't be found on *cls*.
377
+
378
+ attrs.exceptions.NotAnAttrsClassError:
379
+ If *cls* is not an *attrs* class.
380
+
381
+ .. deprecated:: 17.1.0
382
+ Use `attrs.evolve` instead if you can. This function will not be
383
+ removed du to the slightly different approach compared to
384
+ `attrs.evolve`, though.
385
+ """
386
+ new = copy.copy(inst)
387
+ attrs = fields(inst.__class__)
388
+ for k, v in changes.items():
389
+ a = getattr(attrs, k, NOTHING)
390
+ if a is NOTHING:
391
+ msg = f"{k} is not an attrs attribute on {new.__class__}."
392
+ raise AttrsAttributeNotFoundError(msg)
393
+ _OBJ_SETATTR(new, k, v)
394
+ return new
395
+
396
+
397
+ def resolve_types(
398
+ cls, globalns=None, localns=None, attribs=None, include_extras=True
399
+ ):
400
+ """
401
+ Resolve any strings and forward annotations in type annotations.
402
+
403
+ This is only required if you need concrete types in :class:`Attribute`'s
404
+ *type* field. In other words, you don't need to resolve your types if you
405
+ only use them for static type checking.
406
+
407
+ With no arguments, names will be looked up in the module in which the class
408
+ was created. If this is not what you want, for example, if the name only
409
+ exists inside a method, you may pass *globalns* or *localns* to specify
410
+ other dictionaries in which to look up these names. See the docs of
411
+ `typing.get_type_hints` for more details.
412
+
413
+ Args:
414
+ cls (type): Class to resolve.
415
+
416
+ globalns (dict | None): Dictionary containing global variables.
417
+
418
+ localns (dict | None): Dictionary containing local variables.
419
+
420
+ attribs (list | None):
421
+ List of attribs for the given class. This is necessary when calling
422
+ from inside a ``field_transformer`` since *cls* is not an *attrs*
423
+ class yet.
424
+
425
+ include_extras (bool):
426
+ Resolve more accurately, if possible. Pass ``include_extras`` to
427
+ ``typing.get_hints``, if supported by the typing module. On
428
+ supported Python versions (3.9+), this resolves the types more
429
+ accurately.
430
+
431
+ Raises:
432
+ TypeError: If *cls* is not a class.
433
+
434
+ attrs.exceptions.NotAnAttrsClassError:
435
+ If *cls* is not an *attrs* class and you didn't pass any attribs.
436
+
437
+ NameError: If types cannot be resolved because of missing variables.
438
+
439
+ Returns:
440
+ *cls* so you can use this function also as a class decorator. Please
441
+ note that you have to apply it **after** `attrs.define`. That means the
442
+ decorator has to come in the line **before** `attrs.define`.
443
+
444
+ .. versionadded:: 20.1.0
445
+ .. versionadded:: 21.1.0 *attribs*
446
+ .. versionadded:: 23.1.0 *include_extras*
447
+ """
448
+ # Since calling get_type_hints is expensive we cache whether we've
449
+ # done it already.
450
+ if getattr(cls, "__attrs_types_resolved__", None) != cls:
451
+ import typing
452
+
453
+ kwargs = {"globalns": globalns, "localns": localns}
454
+
455
+ if PY_3_9_PLUS:
456
+ kwargs["include_extras"] = include_extras
457
+
458
+ hints = typing.get_type_hints(cls, **kwargs)
459
+ for field in fields(cls) if attribs is None else attribs:
460
+ if field.name in hints:
461
+ # Since fields have been frozen we must work around it.
462
+ _OBJ_SETATTR(field, "type", hints[field.name])
463
+ # We store the class we resolved so that subclasses know they haven't
464
+ # been resolved.
465
+ cls.__attrs_types_resolved__ = cls
466
+
467
+ # Return the class so you can use it as a decorator too.
468
+ return cls
venv/lib/python3.10/site-packages/attr/_make.py ADDED
@@ -0,0 +1,3123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+
3
+ from __future__ import annotations
4
+
5
+ import abc
6
+ import contextlib
7
+ import copy
8
+ import enum
9
+ import inspect
10
+ import itertools
11
+ import linecache
12
+ import sys
13
+ import types
14
+ import unicodedata
15
+
16
+ from collections.abc import Callable, Mapping
17
+ from functools import cached_property
18
+ from typing import Any, NamedTuple, TypeVar
19
+
20
+ # We need to import _compat itself in addition to the _compat members to avoid
21
+ # having the thread-local in the globals here.
22
+ from . import _compat, _config, setters
23
+ from ._compat import (
24
+ PY_3_10_PLUS,
25
+ PY_3_11_PLUS,
26
+ PY_3_13_PLUS,
27
+ _AnnotationExtractor,
28
+ _get_annotations,
29
+ get_generic_base,
30
+ )
31
+ from .exceptions import (
32
+ DefaultAlreadySetError,
33
+ FrozenInstanceError,
34
+ NotAnAttrsClassError,
35
+ UnannotatedAttributeError,
36
+ )
37
+
38
+
39
+ # This is used at least twice, so cache it here.
40
+ _OBJ_SETATTR = object.__setattr__
41
+ _INIT_FACTORY_PAT = "__attr_factory_%s"
42
+ _CLASSVAR_PREFIXES = (
43
+ "typing.ClassVar",
44
+ "t.ClassVar",
45
+ "ClassVar",
46
+ "typing_extensions.ClassVar",
47
+ )
48
+ # we don't use a double-underscore prefix because that triggers
49
+ # name mangling when trying to create a slot for the field
50
+ # (when slots=True)
51
+ _HASH_CACHE_FIELD = "_attrs_cached_hash"
52
+
53
+ _EMPTY_METADATA_SINGLETON = types.MappingProxyType({})
54
+
55
+ # Unique object for unequivocal getattr() defaults.
56
+ _SENTINEL = object()
57
+
58
+ _DEFAULT_ON_SETATTR = setters.pipe(setters.convert, setters.validate)
59
+
60
+
61
+ class _Nothing(enum.Enum):
62
+ """
63
+ Sentinel to indicate the lack of a value when `None` is ambiguous.
64
+
65
+ If extending attrs, you can use ``typing.Literal[NOTHING]`` to show
66
+ that a value may be ``NOTHING``.
67
+
68
+ .. versionchanged:: 21.1.0 ``bool(NOTHING)`` is now False.
69
+ .. versionchanged:: 22.2.0 ``NOTHING`` is now an ``enum.Enum`` variant.
70
+ """
71
+
72
+ NOTHING = enum.auto()
73
+
74
+ def __repr__(self):
75
+ return "NOTHING"
76
+
77
+ def __bool__(self):
78
+ return False
79
+
80
+
81
+ NOTHING = _Nothing.NOTHING
82
+ """
83
+ Sentinel to indicate the lack of a value when `None` is ambiguous.
84
+
85
+ When using in 3rd party code, use `attrs.NothingType` for type annotations.
86
+ """
87
+
88
+
89
+ class _CacheHashWrapper(int):
90
+ """
91
+ An integer subclass that pickles / copies as None
92
+
93
+ This is used for non-slots classes with ``cache_hash=True``, to avoid
94
+ serializing a potentially (even likely) invalid hash value. Since `None`
95
+ is the default value for uncalculated hashes, whenever this is copied,
96
+ the copy's value for the hash should automatically reset.
97
+
98
+ See GH #613 for more details.
99
+ """
100
+
101
+ def __reduce__(self, _none_constructor=type(None), _args=()): # noqa: B008
102
+ return _none_constructor, _args
103
+
104
+
105
+ def attrib(
106
+ default=NOTHING,
107
+ validator=None,
108
+ repr=True,
109
+ cmp=None,
110
+ hash=None,
111
+ init=True,
112
+ metadata=None,
113
+ type=None,
114
+ converter=None,
115
+ factory=None,
116
+ kw_only=False,
117
+ eq=None,
118
+ order=None,
119
+ on_setattr=None,
120
+ alias=None,
121
+ ):
122
+ """
123
+ Create a new field / attribute on a class.
124
+
125
+ Identical to `attrs.field`, except it's not keyword-only.
126
+
127
+ Consider using `attrs.field` in new code (``attr.ib`` will *never* go away,
128
+ though).
129
+
130
+ .. warning::
131
+
132
+ Does **nothing** unless the class is also decorated with
133
+ `attr.s` (or similar)!
134
+
135
+
136
+ .. versionadded:: 15.2.0 *convert*
137
+ .. versionadded:: 16.3.0 *metadata*
138
+ .. versionchanged:: 17.1.0 *validator* can be a ``list`` now.
139
+ .. versionchanged:: 17.1.0
140
+ *hash* is `None` and therefore mirrors *eq* by default.
141
+ .. versionadded:: 17.3.0 *type*
142
+ .. deprecated:: 17.4.0 *convert*
143
+ .. versionadded:: 17.4.0
144
+ *converter* as a replacement for the deprecated *convert* to achieve
145
+ consistency with other noun-based arguments.
146
+ .. versionadded:: 18.1.0
147
+ ``factory=f`` is syntactic sugar for ``default=attr.Factory(f)``.
148
+ .. versionadded:: 18.2.0 *kw_only*
149
+ .. versionchanged:: 19.2.0 *convert* keyword argument removed.
150
+ .. versionchanged:: 19.2.0 *repr* also accepts a custom callable.
151
+ .. deprecated:: 19.2.0 *cmp* Removal on or after 2021-06-01.
152
+ .. versionadded:: 19.2.0 *eq* and *order*
153
+ .. versionadded:: 20.1.0 *on_setattr*
154
+ .. versionchanged:: 20.3.0 *kw_only* backported to Python 2
155
+ .. versionchanged:: 21.1.0
156
+ *eq*, *order*, and *cmp* also accept a custom callable
157
+ .. versionchanged:: 21.1.0 *cmp* undeprecated
158
+ .. versionadded:: 22.2.0 *alias*
159
+ """
160
+ eq, eq_key, order, order_key = _determine_attrib_eq_order(
161
+ cmp, eq, order, True
162
+ )
163
+
164
+ if hash is not None and hash is not True and hash is not False:
165
+ msg = "Invalid value for hash. Must be True, False, or None."
166
+ raise TypeError(msg)
167
+
168
+ if factory is not None:
169
+ if default is not NOTHING:
170
+ msg = (
171
+ "The `default` and `factory` arguments are mutually exclusive."
172
+ )
173
+ raise ValueError(msg)
174
+ if not callable(factory):
175
+ msg = "The `factory` argument must be a callable."
176
+ raise ValueError(msg)
177
+ default = Factory(factory)
178
+
179
+ if metadata is None:
180
+ metadata = {}
181
+
182
+ # Apply syntactic sugar by auto-wrapping.
183
+ if isinstance(on_setattr, (list, tuple)):
184
+ on_setattr = setters.pipe(*on_setattr)
185
+
186
+ if validator and isinstance(validator, (list, tuple)):
187
+ validator = and_(*validator)
188
+
189
+ if converter and isinstance(converter, (list, tuple)):
190
+ converter = pipe(*converter)
191
+
192
+ return _CountingAttr(
193
+ default=default,
194
+ validator=validator,
195
+ repr=repr,
196
+ cmp=None,
197
+ hash=hash,
198
+ init=init,
199
+ converter=converter,
200
+ metadata=metadata,
201
+ type=type,
202
+ kw_only=kw_only,
203
+ eq=eq,
204
+ eq_key=eq_key,
205
+ order=order,
206
+ order_key=order_key,
207
+ on_setattr=on_setattr,
208
+ alias=alias,
209
+ )
210
+
211
+
212
+ def _compile_and_eval(
213
+ script: str,
214
+ globs: dict[str, Any] | None,
215
+ locs: Mapping[str, object] | None = None,
216
+ filename: str = "",
217
+ ) -> None:
218
+ """
219
+ Evaluate the script with the given global (globs) and local (locs)
220
+ variables.
221
+ """
222
+ bytecode = compile(script, filename, "exec")
223
+ eval(bytecode, globs, locs)
224
+
225
+
226
+ def _linecache_and_compile(
227
+ script: str,
228
+ filename: str,
229
+ globs: dict[str, Any] | None,
230
+ locals: Mapping[str, object] | None = None,
231
+ ) -> dict[str, Any]:
232
+ """
233
+ Cache the script with _linecache_, compile it and return the _locals_.
234
+ """
235
+
236
+ locs = {} if locals is None else locals
237
+
238
+ # In order of debuggers like PDB being able to step through the code,
239
+ # we add a fake linecache entry.
240
+ count = 1
241
+ base_filename = filename
242
+ while True:
243
+ linecache_tuple = (
244
+ len(script),
245
+ None,
246
+ script.splitlines(True),
247
+ filename,
248
+ )
249
+ old_val = linecache.cache.setdefault(filename, linecache_tuple)
250
+ if old_val == linecache_tuple:
251
+ break
252
+
253
+ filename = f"{base_filename[:-1]}-{count}>"
254
+ count += 1
255
+
256
+ _compile_and_eval(script, globs, locs, filename)
257
+
258
+ return locs
259
+
260
+
261
+ def _make_attr_tuple_class(cls_name: str, attr_names: list[str]) -> type:
262
+ """
263
+ Create a tuple subclass to hold `Attribute`s for an `attrs` class.
264
+
265
+ The subclass is a bare tuple with properties for names.
266
+
267
+ class MyClassAttributes(tuple):
268
+ __slots__ = ()
269
+ x = property(itemgetter(0))
270
+ """
271
+ attr_class_name = f"{cls_name}Attributes"
272
+ body = {}
273
+ for i, attr_name in enumerate(attr_names):
274
+
275
+ def getter(self, i=i):
276
+ return self[i]
277
+
278
+ body[attr_name] = property(getter)
279
+ return type(attr_class_name, (tuple,), body)
280
+
281
+
282
+ # Tuple class for extracted attributes from a class definition.
283
+ # `base_attrs` is a subset of `attrs`.
284
+ class _Attributes(NamedTuple):
285
+ attrs: type
286
+ base_attrs: list[Attribute]
287
+ base_attrs_map: dict[str, type]
288
+
289
+
290
+ def _is_class_var(annot):
291
+ """
292
+ Check whether *annot* is a typing.ClassVar.
293
+
294
+ The string comparison hack is used to avoid evaluating all string
295
+ annotations which would put attrs-based classes at a performance
296
+ disadvantage compared to plain old classes.
297
+ """
298
+ annot = str(annot)
299
+
300
+ # Annotation can be quoted.
301
+ if annot.startswith(("'", '"')) and annot.endswith(("'", '"')):
302
+ annot = annot[1:-1]
303
+
304
+ return annot.startswith(_CLASSVAR_PREFIXES)
305
+
306
+
307
+ def _has_own_attribute(cls, attrib_name):
308
+ """
309
+ Check whether *cls* defines *attrib_name* (and doesn't just inherit it).
310
+ """
311
+ return attrib_name in cls.__dict__
312
+
313
+
314
+ def _collect_base_attrs(
315
+ cls, taken_attr_names
316
+ ) -> tuple[list[Attribute], dict[str, type]]:
317
+ """
318
+ Collect attr.ibs from base classes of *cls*, except *taken_attr_names*.
319
+ """
320
+ base_attrs = []
321
+ base_attr_map = {} # A dictionary of base attrs to their classes.
322
+
323
+ # Traverse the MRO and collect attributes.
324
+ for base_cls in reversed(cls.__mro__[1:-1]):
325
+ for a in getattr(base_cls, "__attrs_attrs__", []):
326
+ if a.inherited or a.name in taken_attr_names:
327
+ continue
328
+
329
+ a = a.evolve(inherited=True) # noqa: PLW2901
330
+ base_attrs.append(a)
331
+ base_attr_map[a.name] = base_cls
332
+
333
+ # For each name, only keep the freshest definition i.e. the furthest at the
334
+ # back. base_attr_map is fine because it gets overwritten with every new
335
+ # instance.
336
+ filtered = []
337
+ seen = set()
338
+ for a in reversed(base_attrs):
339
+ if a.name in seen:
340
+ continue
341
+ filtered.insert(0, a)
342
+ seen.add(a.name)
343
+
344
+ return filtered, base_attr_map
345
+
346
+
347
+ def _collect_base_attrs_broken(cls, taken_attr_names):
348
+ """
349
+ Collect attr.ibs from base classes of *cls*, except *taken_attr_names*.
350
+
351
+ N.B. *taken_attr_names* will be mutated.
352
+
353
+ Adhere to the old incorrect behavior.
354
+
355
+ Notably it collects from the front and considers inherited attributes which
356
+ leads to the buggy behavior reported in #428.
357
+ """
358
+ base_attrs = []
359
+ base_attr_map = {} # A dictionary of base attrs to their classes.
360
+
361
+ # Traverse the MRO and collect attributes.
362
+ for base_cls in cls.__mro__[1:-1]:
363
+ for a in getattr(base_cls, "__attrs_attrs__", []):
364
+ if a.name in taken_attr_names:
365
+ continue
366
+
367
+ a = a.evolve(inherited=True) # noqa: PLW2901
368
+ taken_attr_names.add(a.name)
369
+ base_attrs.append(a)
370
+ base_attr_map[a.name] = base_cls
371
+
372
+ return base_attrs, base_attr_map
373
+
374
+
375
+ def _transform_attrs(
376
+ cls, these, auto_attribs, kw_only, collect_by_mro, field_transformer
377
+ ) -> _Attributes:
378
+ """
379
+ Transform all `_CountingAttr`s on a class into `Attribute`s.
380
+
381
+ If *these* is passed, use that and don't look for them on the class.
382
+
383
+ If *collect_by_mro* is True, collect them in the correct MRO order,
384
+ otherwise use the old -- incorrect -- order. See #428.
385
+
386
+ Return an `_Attributes`.
387
+ """
388
+ cd = cls.__dict__
389
+ anns = _get_annotations(cls)
390
+
391
+ if these is not None:
392
+ ca_list = list(these.items())
393
+ elif auto_attribs is True:
394
+ ca_names = {
395
+ name
396
+ for name, attr in cd.items()
397
+ if attr.__class__ is _CountingAttr
398
+ }
399
+ ca_list = []
400
+ annot_names = set()
401
+ for attr_name, type in anns.items():
402
+ if _is_class_var(type):
403
+ continue
404
+ annot_names.add(attr_name)
405
+ a = cd.get(attr_name, NOTHING)
406
+
407
+ if a.__class__ is not _CountingAttr:
408
+ a = attrib(a)
409
+ ca_list.append((attr_name, a))
410
+
411
+ unannotated = ca_names - annot_names
412
+ if unannotated:
413
+ raise UnannotatedAttributeError(
414
+ "The following `attr.ib`s lack a type annotation: "
415
+ + ", ".join(
416
+ sorted(unannotated, key=lambda n: cd.get(n).counter)
417
+ )
418
+ + "."
419
+ )
420
+ else:
421
+ ca_list = sorted(
422
+ (
423
+ (name, attr)
424
+ for name, attr in cd.items()
425
+ if attr.__class__ is _CountingAttr
426
+ ),
427
+ key=lambda e: e[1].counter,
428
+ )
429
+
430
+ fca = Attribute.from_counting_attr
431
+ own_attrs = [
432
+ fca(attr_name, ca, anns.get(attr_name)) for attr_name, ca in ca_list
433
+ ]
434
+
435
+ if collect_by_mro:
436
+ base_attrs, base_attr_map = _collect_base_attrs(
437
+ cls, {a.name for a in own_attrs}
438
+ )
439
+ else:
440
+ base_attrs, base_attr_map = _collect_base_attrs_broken(
441
+ cls, {a.name for a in own_attrs}
442
+ )
443
+
444
+ if kw_only:
445
+ own_attrs = [a.evolve(kw_only=True) for a in own_attrs]
446
+ base_attrs = [a.evolve(kw_only=True) for a in base_attrs]
447
+
448
+ attrs = base_attrs + own_attrs
449
+
450
+ if field_transformer is not None:
451
+ attrs = tuple(field_transformer(cls, attrs))
452
+
453
+ # Check attr order after executing the field_transformer.
454
+ # Mandatory vs non-mandatory attr order only matters when they are part of
455
+ # the __init__ signature and when they aren't kw_only (which are moved to
456
+ # the end and can be mandatory or non-mandatory in any order, as they will
457
+ # be specified as keyword args anyway). Check the order of those attrs:
458
+ had_default = False
459
+ for a in (a for a in attrs if a.init is not False and a.kw_only is False):
460
+ if had_default is True and a.default is NOTHING:
461
+ msg = f"No mandatory attributes allowed after an attribute with a default value or factory. Attribute in question: {a!r}"
462
+ raise ValueError(msg)
463
+
464
+ if had_default is False and a.default is not NOTHING:
465
+ had_default = True
466
+
467
+ # Resolve default field alias after executing field_transformer.
468
+ # This allows field_transformer to differentiate between explicit vs
469
+ # default aliases and supply their own defaults.
470
+ for a in attrs:
471
+ if not a.alias:
472
+ # Evolve is very slow, so we hold our nose and do it dirty.
473
+ _OBJ_SETATTR.__get__(a)("alias", _default_init_alias_for(a.name))
474
+
475
+ # Create AttrsClass *after* applying the field_transformer since it may
476
+ # add or remove attributes!
477
+ attr_names = [a.name for a in attrs]
478
+ AttrsClass = _make_attr_tuple_class(cls.__name__, attr_names)
479
+
480
+ return _Attributes(AttrsClass(attrs), base_attrs, base_attr_map)
481
+
482
+
483
+ def _make_cached_property_getattr(cached_properties, original_getattr, cls):
484
+ lines = [
485
+ # Wrapped to get `__class__` into closure cell for super()
486
+ # (It will be replaced with the newly constructed class after construction).
487
+ "def wrapper(_cls):",
488
+ " __class__ = _cls",
489
+ " def __getattr__(self, item, cached_properties=cached_properties, original_getattr=original_getattr, _cached_setattr_get=_cached_setattr_get):",
490
+ " func = cached_properties.get(item)",
491
+ " if func is not None:",
492
+ " result = func(self)",
493
+ " _setter = _cached_setattr_get(self)",
494
+ " _setter(item, result)",
495
+ " return result",
496
+ ]
497
+ if original_getattr is not None:
498
+ lines.append(
499
+ " return original_getattr(self, item)",
500
+ )
501
+ else:
502
+ lines.extend(
503
+ [
504
+ " try:",
505
+ " return super().__getattribute__(item)",
506
+ " except AttributeError:",
507
+ " if not hasattr(super(), '__getattr__'):",
508
+ " raise",
509
+ " return super().__getattr__(item)",
510
+ " original_error = f\"'{self.__class__.__name__}' object has no attribute '{item}'\"",
511
+ " raise AttributeError(original_error)",
512
+ ]
513
+ )
514
+
515
+ lines.extend(
516
+ [
517
+ " return __getattr__",
518
+ "__getattr__ = wrapper(_cls)",
519
+ ]
520
+ )
521
+
522
+ unique_filename = _generate_unique_filename(cls, "getattr")
523
+
524
+ glob = {
525
+ "cached_properties": cached_properties,
526
+ "_cached_setattr_get": _OBJ_SETATTR.__get__,
527
+ "original_getattr": original_getattr,
528
+ }
529
+
530
+ return _linecache_and_compile(
531
+ "\n".join(lines), unique_filename, glob, locals={"_cls": cls}
532
+ )["__getattr__"]
533
+
534
+
535
+ def _frozen_setattrs(self, name, value):
536
+ """
537
+ Attached to frozen classes as __setattr__.
538
+ """
539
+ if isinstance(self, BaseException) and name in (
540
+ "__cause__",
541
+ "__context__",
542
+ "__traceback__",
543
+ "__suppress_context__",
544
+ "__notes__",
545
+ ):
546
+ BaseException.__setattr__(self, name, value)
547
+ return
548
+
549
+ raise FrozenInstanceError
550
+
551
+
552
+ def _frozen_delattrs(self, name):
553
+ """
554
+ Attached to frozen classes as __delattr__.
555
+ """
556
+ if isinstance(self, BaseException) and name in ("__notes__",):
557
+ BaseException.__delattr__(self, name)
558
+ return
559
+
560
+ raise FrozenInstanceError
561
+
562
+
563
+ def evolve(*args, **changes):
564
+ """
565
+ Create a new instance, based on the first positional argument with
566
+ *changes* applied.
567
+
568
+ .. tip::
569
+
570
+ On Python 3.13 and later, you can also use `copy.replace` instead.
571
+
572
+ Args:
573
+
574
+ inst:
575
+ Instance of a class with *attrs* attributes. *inst* must be passed
576
+ as a positional argument.
577
+
578
+ changes:
579
+ Keyword changes in the new copy.
580
+
581
+ Returns:
582
+ A copy of inst with *changes* incorporated.
583
+
584
+ Raises:
585
+ TypeError:
586
+ If *attr_name* couldn't be found in the class ``__init__``.
587
+
588
+ attrs.exceptions.NotAnAttrsClassError:
589
+ If *cls* is not an *attrs* class.
590
+
591
+ .. versionadded:: 17.1.0
592
+ .. deprecated:: 23.1.0
593
+ It is now deprecated to pass the instance using the keyword argument
594
+ *inst*. It will raise a warning until at least April 2024, after which
595
+ it will become an error. Always pass the instance as a positional
596
+ argument.
597
+ .. versionchanged:: 24.1.0
598
+ *inst* can't be passed as a keyword argument anymore.
599
+ """
600
+ try:
601
+ (inst,) = args
602
+ except ValueError:
603
+ msg = (
604
+ f"evolve() takes 1 positional argument, but {len(args)} were given"
605
+ )
606
+ raise TypeError(msg) from None
607
+
608
+ cls = inst.__class__
609
+ attrs = fields(cls)
610
+ for a in attrs:
611
+ if not a.init:
612
+ continue
613
+ attr_name = a.name # To deal with private attributes.
614
+ init_name = a.alias
615
+ if init_name not in changes:
616
+ changes[init_name] = getattr(inst, attr_name)
617
+
618
+ return cls(**changes)
619
+
620
+
621
+ class _ClassBuilder:
622
+ """
623
+ Iteratively build *one* class.
624
+ """
625
+
626
+ __slots__ = (
627
+ "_add_method_dunders",
628
+ "_attr_names",
629
+ "_attrs",
630
+ "_base_attr_map",
631
+ "_base_names",
632
+ "_cache_hash",
633
+ "_cls",
634
+ "_cls_dict",
635
+ "_delete_attribs",
636
+ "_frozen",
637
+ "_has_custom_setattr",
638
+ "_has_post_init",
639
+ "_has_pre_init",
640
+ "_is_exc",
641
+ "_on_setattr",
642
+ "_pre_init_has_args",
643
+ "_repr_added",
644
+ "_script_snippets",
645
+ "_slots",
646
+ "_weakref_slot",
647
+ "_wrote_own_setattr",
648
+ )
649
+
650
+ def __init__(
651
+ self,
652
+ cls: type,
653
+ these,
654
+ slots,
655
+ frozen,
656
+ weakref_slot,
657
+ getstate_setstate,
658
+ auto_attribs,
659
+ kw_only,
660
+ cache_hash,
661
+ is_exc,
662
+ collect_by_mro,
663
+ on_setattr,
664
+ has_custom_setattr,
665
+ field_transformer,
666
+ ):
667
+ attrs, base_attrs, base_map = _transform_attrs(
668
+ cls,
669
+ these,
670
+ auto_attribs,
671
+ kw_only,
672
+ collect_by_mro,
673
+ field_transformer,
674
+ )
675
+
676
+ self._cls = cls
677
+ self._cls_dict = dict(cls.__dict__) if slots else {}
678
+ self._attrs = attrs
679
+ self._base_names = {a.name for a in base_attrs}
680
+ self._base_attr_map = base_map
681
+ self._attr_names = tuple(a.name for a in attrs)
682
+ self._slots = slots
683
+ self._frozen = frozen
684
+ self._weakref_slot = weakref_slot
685
+ self._cache_hash = cache_hash
686
+ self._has_pre_init = bool(getattr(cls, "__attrs_pre_init__", False))
687
+ self._pre_init_has_args = False
688
+ if self._has_pre_init:
689
+ # Check if the pre init method has more arguments than just `self`
690
+ # We want to pass arguments if pre init expects arguments
691
+ pre_init_func = cls.__attrs_pre_init__
692
+ pre_init_signature = inspect.signature(pre_init_func)
693
+ self._pre_init_has_args = len(pre_init_signature.parameters) > 1
694
+ self._has_post_init = bool(getattr(cls, "__attrs_post_init__", False))
695
+ self._delete_attribs = not bool(these)
696
+ self._is_exc = is_exc
697
+ self._on_setattr = on_setattr
698
+
699
+ self._has_custom_setattr = has_custom_setattr
700
+ self._wrote_own_setattr = False
701
+
702
+ self._cls_dict["__attrs_attrs__"] = self._attrs
703
+
704
+ if frozen:
705
+ self._cls_dict["__setattr__"] = _frozen_setattrs
706
+ self._cls_dict["__delattr__"] = _frozen_delattrs
707
+
708
+ self._wrote_own_setattr = True
709
+ elif on_setattr in (
710
+ _DEFAULT_ON_SETATTR,
711
+ setters.validate,
712
+ setters.convert,
713
+ ):
714
+ has_validator = has_converter = False
715
+ for a in attrs:
716
+ if a.validator is not None:
717
+ has_validator = True
718
+ if a.converter is not None:
719
+ has_converter = True
720
+
721
+ if has_validator and has_converter:
722
+ break
723
+ if (
724
+ (
725
+ on_setattr == _DEFAULT_ON_SETATTR
726
+ and not (has_validator or has_converter)
727
+ )
728
+ or (on_setattr == setters.validate and not has_validator)
729
+ or (on_setattr == setters.convert and not has_converter)
730
+ ):
731
+ # If class-level on_setattr is set to convert + validate, but
732
+ # there's no field to convert or validate, pretend like there's
733
+ # no on_setattr.
734
+ self._on_setattr = None
735
+
736
+ if getstate_setstate:
737
+ (
738
+ self._cls_dict["__getstate__"],
739
+ self._cls_dict["__setstate__"],
740
+ ) = self._make_getstate_setstate()
741
+
742
+ # tuples of script, globs, hook
743
+ self._script_snippets: list[
744
+ tuple[str, dict, Callable[[dict, dict], Any]]
745
+ ] = []
746
+ self._repr_added = False
747
+
748
+ # We want to only do this check once; in 99.9% of cases these
749
+ # exist.
750
+ if not hasattr(self._cls, "__module__") or not hasattr(
751
+ self._cls, "__qualname__"
752
+ ):
753
+ self._add_method_dunders = self._add_method_dunders_safe
754
+ else:
755
+ self._add_method_dunders = self._add_method_dunders_unsafe
756
+
757
+ def __repr__(self):
758
+ return f"<_ClassBuilder(cls={self._cls.__name__})>"
759
+
760
+ def _eval_snippets(self) -> None:
761
+ """
762
+ Evaluate any registered snippets in one go.
763
+ """
764
+ script = "\n".join([snippet[0] for snippet in self._script_snippets])
765
+ globs = {}
766
+ for _, snippet_globs, _ in self._script_snippets:
767
+ globs.update(snippet_globs)
768
+
769
+ locs = _linecache_and_compile(
770
+ script,
771
+ _generate_unique_filename(self._cls, "methods"),
772
+ globs,
773
+ )
774
+
775
+ for _, _, hook in self._script_snippets:
776
+ hook(self._cls_dict, locs)
777
+
778
+ def build_class(self):
779
+ """
780
+ Finalize class based on the accumulated configuration.
781
+
782
+ Builder cannot be used after calling this method.
783
+ """
784
+ self._eval_snippets()
785
+ if self._slots is True:
786
+ cls = self._create_slots_class()
787
+ else:
788
+ cls = self._patch_original_class()
789
+ if PY_3_10_PLUS:
790
+ cls = abc.update_abstractmethods(cls)
791
+
792
+ # The method gets only called if it's not inherited from a base class.
793
+ # _has_own_attribute does NOT work properly for classmethods.
794
+ if (
795
+ getattr(cls, "__attrs_init_subclass__", None)
796
+ and "__attrs_init_subclass__" not in cls.__dict__
797
+ ):
798
+ cls.__attrs_init_subclass__()
799
+
800
+ return cls
801
+
802
+ def _patch_original_class(self):
803
+ """
804
+ Apply accumulated methods and return the class.
805
+ """
806
+ cls = self._cls
807
+ base_names = self._base_names
808
+
809
+ # Clean class of attribute definitions (`attr.ib()`s).
810
+ if self._delete_attribs:
811
+ for name in self._attr_names:
812
+ if (
813
+ name not in base_names
814
+ and getattr(cls, name, _SENTINEL) is not _SENTINEL
815
+ ):
816
+ # An AttributeError can happen if a base class defines a
817
+ # class variable and we want to set an attribute with the
818
+ # same name by using only a type annotation.
819
+ with contextlib.suppress(AttributeError):
820
+ delattr(cls, name)
821
+
822
+ # Attach our dunder methods.
823
+ for name, value in self._cls_dict.items():
824
+ setattr(cls, name, value)
825
+
826
+ # If we've inherited an attrs __setattr__ and don't write our own,
827
+ # reset it to object's.
828
+ if not self._wrote_own_setattr and getattr(
829
+ cls, "__attrs_own_setattr__", False
830
+ ):
831
+ cls.__attrs_own_setattr__ = False
832
+
833
+ if not self._has_custom_setattr:
834
+ cls.__setattr__ = _OBJ_SETATTR
835
+
836
+ return cls
837
+
838
+ def _create_slots_class(self):
839
+ """
840
+ Build and return a new class with a `__slots__` attribute.
841
+ """
842
+ cd = {
843
+ k: v
844
+ for k, v in self._cls_dict.items()
845
+ if k not in (*tuple(self._attr_names), "__dict__", "__weakref__")
846
+ }
847
+
848
+ # If our class doesn't have its own implementation of __setattr__
849
+ # (either from the user or by us), check the bases, if one of them has
850
+ # an attrs-made __setattr__, that needs to be reset. We don't walk the
851
+ # MRO because we only care about our immediate base classes.
852
+ # XXX: This can be confused by subclassing a slotted attrs class with
853
+ # XXX: a non-attrs class and subclass the resulting class with an attrs
854
+ # XXX: class. See `test_slotted_confused` for details. For now that's
855
+ # XXX: OK with us.
856
+ if not self._wrote_own_setattr:
857
+ cd["__attrs_own_setattr__"] = False
858
+
859
+ if not self._has_custom_setattr:
860
+ for base_cls in self._cls.__bases__:
861
+ if base_cls.__dict__.get("__attrs_own_setattr__", False):
862
+ cd["__setattr__"] = _OBJ_SETATTR
863
+ break
864
+
865
+ # Traverse the MRO to collect existing slots
866
+ # and check for an existing __weakref__.
867
+ existing_slots = {}
868
+ weakref_inherited = False
869
+ for base_cls in self._cls.__mro__[1:-1]:
870
+ if base_cls.__dict__.get("__weakref__", None) is not None:
871
+ weakref_inherited = True
872
+ existing_slots.update(
873
+ {
874
+ name: getattr(base_cls, name)
875
+ for name in getattr(base_cls, "__slots__", [])
876
+ }
877
+ )
878
+
879
+ base_names = set(self._base_names)
880
+
881
+ names = self._attr_names
882
+ if (
883
+ self._weakref_slot
884
+ and "__weakref__" not in getattr(self._cls, "__slots__", ())
885
+ and "__weakref__" not in names
886
+ and not weakref_inherited
887
+ ):
888
+ names += ("__weakref__",)
889
+
890
+ cached_properties = {
891
+ name: cached_prop.func
892
+ for name, cached_prop in cd.items()
893
+ if isinstance(cached_prop, cached_property)
894
+ }
895
+
896
+ # Collect methods with a `__class__` reference that are shadowed in the new class.
897
+ # To know to update them.
898
+ additional_closure_functions_to_update = []
899
+ if cached_properties:
900
+ class_annotations = _get_annotations(self._cls)
901
+ for name, func in cached_properties.items():
902
+ # Add cached properties to names for slotting.
903
+ names += (name,)
904
+ # Clear out function from class to avoid clashing.
905
+ del cd[name]
906
+ additional_closure_functions_to_update.append(func)
907
+ annotation = inspect.signature(func).return_annotation
908
+ if annotation is not inspect.Parameter.empty:
909
+ class_annotations[name] = annotation
910
+
911
+ original_getattr = cd.get("__getattr__")
912
+ if original_getattr is not None:
913
+ additional_closure_functions_to_update.append(original_getattr)
914
+
915
+ cd["__getattr__"] = _make_cached_property_getattr(
916
+ cached_properties, original_getattr, self._cls
917
+ )
918
+
919
+ # We only add the names of attributes that aren't inherited.
920
+ # Setting __slots__ to inherited attributes wastes memory.
921
+ slot_names = [name for name in names if name not in base_names]
922
+
923
+ # There are slots for attributes from current class
924
+ # that are defined in parent classes.
925
+ # As their descriptors may be overridden by a child class,
926
+ # we collect them here and update the class dict
927
+ reused_slots = {
928
+ slot: slot_descriptor
929
+ for slot, slot_descriptor in existing_slots.items()
930
+ if slot in slot_names
931
+ }
932
+ slot_names = [name for name in slot_names if name not in reused_slots]
933
+ cd.update(reused_slots)
934
+ if self._cache_hash:
935
+ slot_names.append(_HASH_CACHE_FIELD)
936
+
937
+ cd["__slots__"] = tuple(slot_names)
938
+
939
+ cd["__qualname__"] = self._cls.__qualname__
940
+
941
+ # Create new class based on old class and our methods.
942
+ cls = type(self._cls)(self._cls.__name__, self._cls.__bases__, cd)
943
+
944
+ # The following is a fix for
945
+ # <https://github.com/python-attrs/attrs/issues/102>.
946
+ # If a method mentions `__class__` or uses the no-arg super(), the
947
+ # compiler will bake a reference to the class in the method itself
948
+ # as `method.__closure__`. Since we replace the class with a
949
+ # clone, we rewrite these references so it keeps working.
950
+ for item in itertools.chain(
951
+ cls.__dict__.values(), additional_closure_functions_to_update
952
+ ):
953
+ if isinstance(item, (classmethod, staticmethod)):
954
+ # Class- and staticmethods hide their functions inside.
955
+ # These might need to be rewritten as well.
956
+ closure_cells = getattr(item.__func__, "__closure__", None)
957
+ elif isinstance(item, property):
958
+ # Workaround for property `super()` shortcut (PY3-only).
959
+ # There is no universal way for other descriptors.
960
+ closure_cells = getattr(item.fget, "__closure__", None)
961
+ else:
962
+ closure_cells = getattr(item, "__closure__", None)
963
+
964
+ if not closure_cells: # Catch None or the empty list.
965
+ continue
966
+ for cell in closure_cells:
967
+ try:
968
+ match = cell.cell_contents is self._cls
969
+ except ValueError: # noqa: PERF203
970
+ # ValueError: Cell is empty
971
+ pass
972
+ else:
973
+ if match:
974
+ cell.cell_contents = cls
975
+ return cls
976
+
977
+ def add_repr(self, ns):
978
+ script, globs = _make_repr_script(self._attrs, ns)
979
+
980
+ def _attach_repr(cls_dict, globs):
981
+ cls_dict["__repr__"] = self._add_method_dunders(globs["__repr__"])
982
+
983
+ self._script_snippets.append((script, globs, _attach_repr))
984
+ self._repr_added = True
985
+ return self
986
+
987
+ def add_str(self):
988
+ if not self._repr_added:
989
+ msg = "__str__ can only be generated if a __repr__ exists."
990
+ raise ValueError(msg)
991
+
992
+ def __str__(self):
993
+ return self.__repr__()
994
+
995
+ self._cls_dict["__str__"] = self._add_method_dunders(__str__)
996
+ return self
997
+
998
+ def _make_getstate_setstate(self):
999
+ """
1000
+ Create custom __setstate__ and __getstate__ methods.
1001
+ """
1002
+ # __weakref__ is not writable.
1003
+ state_attr_names = tuple(
1004
+ an for an in self._attr_names if an != "__weakref__"
1005
+ )
1006
+
1007
+ def slots_getstate(self):
1008
+ """
1009
+ Automatically created by attrs.
1010
+ """
1011
+ return {name: getattr(self, name) for name in state_attr_names}
1012
+
1013
+ hash_caching_enabled = self._cache_hash
1014
+
1015
+ def slots_setstate(self, state):
1016
+ """
1017
+ Automatically created by attrs.
1018
+ """
1019
+ __bound_setattr = _OBJ_SETATTR.__get__(self)
1020
+ if isinstance(state, tuple):
1021
+ # Backward compatibility with attrs instances pickled with
1022
+ # attrs versions before v22.2.0 which stored tuples.
1023
+ for name, value in zip(state_attr_names, state):
1024
+ __bound_setattr(name, value)
1025
+ else:
1026
+ for name in state_attr_names:
1027
+ if name in state:
1028
+ __bound_setattr(name, state[name])
1029
+
1030
+ # The hash code cache is not included when the object is
1031
+ # serialized, but it still needs to be initialized to None to
1032
+ # indicate that the first call to __hash__ should be a cache
1033
+ # miss.
1034
+ if hash_caching_enabled:
1035
+ __bound_setattr(_HASH_CACHE_FIELD, None)
1036
+
1037
+ return slots_getstate, slots_setstate
1038
+
1039
+ def make_unhashable(self):
1040
+ self._cls_dict["__hash__"] = None
1041
+ return self
1042
+
1043
+ def add_hash(self):
1044
+ script, globs = _make_hash_script(
1045
+ self._cls,
1046
+ self._attrs,
1047
+ frozen=self._frozen,
1048
+ cache_hash=self._cache_hash,
1049
+ )
1050
+
1051
+ def attach_hash(cls_dict: dict, locs: dict) -> None:
1052
+ cls_dict["__hash__"] = self._add_method_dunders(locs["__hash__"])
1053
+
1054
+ self._script_snippets.append((script, globs, attach_hash))
1055
+
1056
+ return self
1057
+
1058
+ def add_init(self):
1059
+ script, globs, annotations = _make_init_script(
1060
+ self._cls,
1061
+ self._attrs,
1062
+ self._has_pre_init,
1063
+ self._pre_init_has_args,
1064
+ self._has_post_init,
1065
+ self._frozen,
1066
+ self._slots,
1067
+ self._cache_hash,
1068
+ self._base_attr_map,
1069
+ self._is_exc,
1070
+ self._on_setattr,
1071
+ attrs_init=False,
1072
+ )
1073
+
1074
+ def _attach_init(cls_dict, globs):
1075
+ init = globs["__init__"]
1076
+ init.__annotations__ = annotations
1077
+ cls_dict["__init__"] = self._add_method_dunders(init)
1078
+
1079
+ self._script_snippets.append((script, globs, _attach_init))
1080
+
1081
+ return self
1082
+
1083
+ def add_replace(self):
1084
+ self._cls_dict["__replace__"] = self._add_method_dunders(
1085
+ lambda self, **changes: evolve(self, **changes)
1086
+ )
1087
+ return self
1088
+
1089
+ def add_match_args(self):
1090
+ self._cls_dict["__match_args__"] = tuple(
1091
+ field.name
1092
+ for field in self._attrs
1093
+ if field.init and not field.kw_only
1094
+ )
1095
+
1096
+ def add_attrs_init(self):
1097
+ script, globs, annotations = _make_init_script(
1098
+ self._cls,
1099
+ self._attrs,
1100
+ self._has_pre_init,
1101
+ self._pre_init_has_args,
1102
+ self._has_post_init,
1103
+ self._frozen,
1104
+ self._slots,
1105
+ self._cache_hash,
1106
+ self._base_attr_map,
1107
+ self._is_exc,
1108
+ self._on_setattr,
1109
+ attrs_init=True,
1110
+ )
1111
+
1112
+ def _attach_attrs_init(cls_dict, globs):
1113
+ init = globs["__attrs_init__"]
1114
+ init.__annotations__ = annotations
1115
+ cls_dict["__attrs_init__"] = self._add_method_dunders(init)
1116
+
1117
+ self._script_snippets.append((script, globs, _attach_attrs_init))
1118
+
1119
+ return self
1120
+
1121
+ def add_eq(self):
1122
+ cd = self._cls_dict
1123
+
1124
+ script, globs = _make_eq_script(self._attrs)
1125
+
1126
+ def _attach_eq(cls_dict, globs):
1127
+ cls_dict["__eq__"] = self._add_method_dunders(globs["__eq__"])
1128
+
1129
+ self._script_snippets.append((script, globs, _attach_eq))
1130
+
1131
+ cd["__ne__"] = __ne__
1132
+
1133
+ return self
1134
+
1135
+ def add_order(self):
1136
+ cd = self._cls_dict
1137
+
1138
+ cd["__lt__"], cd["__le__"], cd["__gt__"], cd["__ge__"] = (
1139
+ self._add_method_dunders(meth)
1140
+ for meth in _make_order(self._cls, self._attrs)
1141
+ )
1142
+
1143
+ return self
1144
+
1145
+ def add_setattr(self):
1146
+ sa_attrs = {}
1147
+ for a in self._attrs:
1148
+ on_setattr = a.on_setattr or self._on_setattr
1149
+ if on_setattr and on_setattr is not setters.NO_OP:
1150
+ sa_attrs[a.name] = a, on_setattr
1151
+
1152
+ if not sa_attrs:
1153
+ return self
1154
+
1155
+ if self._has_custom_setattr:
1156
+ # We need to write a __setattr__ but there already is one!
1157
+ msg = "Can't combine custom __setattr__ with on_setattr hooks."
1158
+ raise ValueError(msg)
1159
+
1160
+ # docstring comes from _add_method_dunders
1161
+ def __setattr__(self, name, val):
1162
+ try:
1163
+ a, hook = sa_attrs[name]
1164
+ except KeyError:
1165
+ nval = val
1166
+ else:
1167
+ nval = hook(self, a, val)
1168
+
1169
+ _OBJ_SETATTR(self, name, nval)
1170
+
1171
+ self._cls_dict["__attrs_own_setattr__"] = True
1172
+ self._cls_dict["__setattr__"] = self._add_method_dunders(__setattr__)
1173
+ self._wrote_own_setattr = True
1174
+
1175
+ return self
1176
+
1177
+ def _add_method_dunders_unsafe(self, method: Callable) -> Callable:
1178
+ """
1179
+ Add __module__ and __qualname__ to a *method*.
1180
+ """
1181
+ method.__module__ = self._cls.__module__
1182
+
1183
+ method.__qualname__ = f"{self._cls.__qualname__}.{method.__name__}"
1184
+
1185
+ method.__doc__ = (
1186
+ f"Method generated by attrs for class {self._cls.__qualname__}."
1187
+ )
1188
+
1189
+ return method
1190
+
1191
+ def _add_method_dunders_safe(self, method: Callable) -> Callable:
1192
+ """
1193
+ Add __module__ and __qualname__ to a *method* if possible.
1194
+ """
1195
+ with contextlib.suppress(AttributeError):
1196
+ method.__module__ = self._cls.__module__
1197
+
1198
+ with contextlib.suppress(AttributeError):
1199
+ method.__qualname__ = f"{self._cls.__qualname__}.{method.__name__}"
1200
+
1201
+ with contextlib.suppress(AttributeError):
1202
+ method.__doc__ = f"Method generated by attrs for class {self._cls.__qualname__}."
1203
+
1204
+ return method
1205
+
1206
+
1207
+ def _determine_attrs_eq_order(cmp, eq, order, default_eq):
1208
+ """
1209
+ Validate the combination of *cmp*, *eq*, and *order*. Derive the effective
1210
+ values of eq and order. If *eq* is None, set it to *default_eq*.
1211
+ """
1212
+ if cmp is not None and any((eq is not None, order is not None)):
1213
+ msg = "Don't mix `cmp` with `eq' and `order`."
1214
+ raise ValueError(msg)
1215
+
1216
+ # cmp takes precedence due to bw-compatibility.
1217
+ if cmp is not None:
1218
+ return cmp, cmp
1219
+
1220
+ # If left None, equality is set to the specified default and ordering
1221
+ # mirrors equality.
1222
+ if eq is None:
1223
+ eq = default_eq
1224
+
1225
+ if order is None:
1226
+ order = eq
1227
+
1228
+ if eq is False and order is True:
1229
+ msg = "`order` can only be True if `eq` is True too."
1230
+ raise ValueError(msg)
1231
+
1232
+ return eq, order
1233
+
1234
+
1235
+ def _determine_attrib_eq_order(cmp, eq, order, default_eq):
1236
+ """
1237
+ Validate the combination of *cmp*, *eq*, and *order*. Derive the effective
1238
+ values of eq and order. If *eq* is None, set it to *default_eq*.
1239
+ """
1240
+ if cmp is not None and any((eq is not None, order is not None)):
1241
+ msg = "Don't mix `cmp` with `eq' and `order`."
1242
+ raise ValueError(msg)
1243
+
1244
+ def decide_callable_or_boolean(value):
1245
+ """
1246
+ Decide whether a key function is used.
1247
+ """
1248
+ if callable(value):
1249
+ value, key = True, value
1250
+ else:
1251
+ key = None
1252
+ return value, key
1253
+
1254
+ # cmp takes precedence due to bw-compatibility.
1255
+ if cmp is not None:
1256
+ cmp, cmp_key = decide_callable_or_boolean(cmp)
1257
+ return cmp, cmp_key, cmp, cmp_key
1258
+
1259
+ # If left None, equality is set to the specified default and ordering
1260
+ # mirrors equality.
1261
+ if eq is None:
1262
+ eq, eq_key = default_eq, None
1263
+ else:
1264
+ eq, eq_key = decide_callable_or_boolean(eq)
1265
+
1266
+ if order is None:
1267
+ order, order_key = eq, eq_key
1268
+ else:
1269
+ order, order_key = decide_callable_or_boolean(order)
1270
+
1271
+ if eq is False and order is True:
1272
+ msg = "`order` can only be True if `eq` is True too."
1273
+ raise ValueError(msg)
1274
+
1275
+ return eq, eq_key, order, order_key
1276
+
1277
+
1278
+ def _determine_whether_to_implement(
1279
+ cls, flag, auto_detect, dunders, default=True
1280
+ ):
1281
+ """
1282
+ Check whether we should implement a set of methods for *cls*.
1283
+
1284
+ *flag* is the argument passed into @attr.s like 'init', *auto_detect* the
1285
+ same as passed into @attr.s and *dunders* is a tuple of attribute names
1286
+ whose presence signal that the user has implemented it themselves.
1287
+
1288
+ Return *default* if no reason for either for or against is found.
1289
+ """
1290
+ if flag is True or flag is False:
1291
+ return flag
1292
+
1293
+ if flag is None and auto_detect is False:
1294
+ return default
1295
+
1296
+ # Logically, flag is None and auto_detect is True here.
1297
+ for dunder in dunders:
1298
+ if _has_own_attribute(cls, dunder):
1299
+ return False
1300
+
1301
+ return default
1302
+
1303
+
1304
+ def attrs(
1305
+ maybe_cls=None,
1306
+ these=None,
1307
+ repr_ns=None,
1308
+ repr=None,
1309
+ cmp=None,
1310
+ hash=None,
1311
+ init=None,
1312
+ slots=False,
1313
+ frozen=False,
1314
+ weakref_slot=True,
1315
+ str=False,
1316
+ auto_attribs=False,
1317
+ kw_only=False,
1318
+ cache_hash=False,
1319
+ auto_exc=False,
1320
+ eq=None,
1321
+ order=None,
1322
+ auto_detect=False,
1323
+ collect_by_mro=False,
1324
+ getstate_setstate=None,
1325
+ on_setattr=None,
1326
+ field_transformer=None,
1327
+ match_args=True,
1328
+ unsafe_hash=None,
1329
+ ):
1330
+ r"""
1331
+ A class decorator that adds :term:`dunder methods` according to the
1332
+ specified attributes using `attr.ib` or the *these* argument.
1333
+
1334
+ Consider using `attrs.define` / `attrs.frozen` in new code (``attr.s`` will
1335
+ *never* go away, though).
1336
+
1337
+ Args:
1338
+ repr_ns (str):
1339
+ When using nested classes, there was no way in Python 2 to
1340
+ automatically detect that. This argument allows to set a custom
1341
+ name for a more meaningful ``repr`` output. This argument is
1342
+ pointless in Python 3 and is therefore deprecated.
1343
+
1344
+ .. caution::
1345
+ Refer to `attrs.define` for the rest of the parameters, but note that they
1346
+ can have different defaults.
1347
+
1348
+ Notably, leaving *on_setattr* as `None` will **not** add any hooks.
1349
+
1350
+ .. versionadded:: 16.0.0 *slots*
1351
+ .. versionadded:: 16.1.0 *frozen*
1352
+ .. versionadded:: 16.3.0 *str*
1353
+ .. versionadded:: 16.3.0 Support for ``__attrs_post_init__``.
1354
+ .. versionchanged:: 17.1.0
1355
+ *hash* supports `None` as value which is also the default now.
1356
+ .. versionadded:: 17.3.0 *auto_attribs*
1357
+ .. versionchanged:: 18.1.0
1358
+ If *these* is passed, no attributes are deleted from the class body.
1359
+ .. versionchanged:: 18.1.0 If *these* is ordered, the order is retained.
1360
+ .. versionadded:: 18.2.0 *weakref_slot*
1361
+ .. deprecated:: 18.2.0
1362
+ ``__lt__``, ``__le__``, ``__gt__``, and ``__ge__`` now raise a
1363
+ `DeprecationWarning` if the classes compared are subclasses of
1364
+ each other. ``__eq`` and ``__ne__`` never tried to compared subclasses
1365
+ to each other.
1366
+ .. versionchanged:: 19.2.0
1367
+ ``__lt__``, ``__le__``, ``__gt__``, and ``__ge__`` now do not consider
1368
+ subclasses comparable anymore.
1369
+ .. versionadded:: 18.2.0 *kw_only*
1370
+ .. versionadded:: 18.2.0 *cache_hash*
1371
+ .. versionadded:: 19.1.0 *auto_exc*
1372
+ .. deprecated:: 19.2.0 *cmp* Removal on or after 2021-06-01.
1373
+ .. versionadded:: 19.2.0 *eq* and *order*
1374
+ .. versionadded:: 20.1.0 *auto_detect*
1375
+ .. versionadded:: 20.1.0 *collect_by_mro*
1376
+ .. versionadded:: 20.1.0 *getstate_setstate*
1377
+ .. versionadded:: 20.1.0 *on_setattr*
1378
+ .. versionadded:: 20.3.0 *field_transformer*
1379
+ .. versionchanged:: 21.1.0
1380
+ ``init=False`` injects ``__attrs_init__``
1381
+ .. versionchanged:: 21.1.0 Support for ``__attrs_pre_init__``
1382
+ .. versionchanged:: 21.1.0 *cmp* undeprecated
1383
+ .. versionadded:: 21.3.0 *match_args*
1384
+ .. versionadded:: 22.2.0
1385
+ *unsafe_hash* as an alias for *hash* (for :pep:`681` compliance).
1386
+ .. deprecated:: 24.1.0 *repr_ns*
1387
+ .. versionchanged:: 24.1.0
1388
+ Instances are not compared as tuples of attributes anymore, but using a
1389
+ big ``and`` condition. This is faster and has more correct behavior for
1390
+ uncomparable values like `math.nan`.
1391
+ .. versionadded:: 24.1.0
1392
+ If a class has an *inherited* classmethod called
1393
+ ``__attrs_init_subclass__``, it is executed after the class is created.
1394
+ .. deprecated:: 24.1.0 *hash* is deprecated in favor of *unsafe_hash*.
1395
+ """
1396
+ if repr_ns is not None:
1397
+ import warnings
1398
+
1399
+ warnings.warn(
1400
+ DeprecationWarning(
1401
+ "The `repr_ns` argument is deprecated and will be removed in or after August 2025."
1402
+ ),
1403
+ stacklevel=2,
1404
+ )
1405
+
1406
+ eq_, order_ = _determine_attrs_eq_order(cmp, eq, order, None)
1407
+
1408
+ # unsafe_hash takes precedence due to PEP 681.
1409
+ if unsafe_hash is not None:
1410
+ hash = unsafe_hash
1411
+
1412
+ if isinstance(on_setattr, (list, tuple)):
1413
+ on_setattr = setters.pipe(*on_setattr)
1414
+
1415
+ def wrap(cls):
1416
+ is_frozen = frozen or _has_frozen_base_class(cls)
1417
+ is_exc = auto_exc is True and issubclass(cls, BaseException)
1418
+ has_own_setattr = auto_detect and _has_own_attribute(
1419
+ cls, "__setattr__"
1420
+ )
1421
+
1422
+ if has_own_setattr and is_frozen:
1423
+ msg = "Can't freeze a class with a custom __setattr__."
1424
+ raise ValueError(msg)
1425
+
1426
+ builder = _ClassBuilder(
1427
+ cls,
1428
+ these,
1429
+ slots,
1430
+ is_frozen,
1431
+ weakref_slot,
1432
+ _determine_whether_to_implement(
1433
+ cls,
1434
+ getstate_setstate,
1435
+ auto_detect,
1436
+ ("__getstate__", "__setstate__"),
1437
+ default=slots,
1438
+ ),
1439
+ auto_attribs,
1440
+ kw_only,
1441
+ cache_hash,
1442
+ is_exc,
1443
+ collect_by_mro,
1444
+ on_setattr,
1445
+ has_own_setattr,
1446
+ field_transformer,
1447
+ )
1448
+
1449
+ if _determine_whether_to_implement(
1450
+ cls, repr, auto_detect, ("__repr__",)
1451
+ ):
1452
+ builder.add_repr(repr_ns)
1453
+
1454
+ if str is True:
1455
+ builder.add_str()
1456
+
1457
+ eq = _determine_whether_to_implement(
1458
+ cls, eq_, auto_detect, ("__eq__", "__ne__")
1459
+ )
1460
+ if not is_exc and eq is True:
1461
+ builder.add_eq()
1462
+ if not is_exc and _determine_whether_to_implement(
1463
+ cls, order_, auto_detect, ("__lt__", "__le__", "__gt__", "__ge__")
1464
+ ):
1465
+ builder.add_order()
1466
+
1467
+ if not frozen:
1468
+ builder.add_setattr()
1469
+
1470
+ nonlocal hash
1471
+ if (
1472
+ hash is None
1473
+ and auto_detect is True
1474
+ and _has_own_attribute(cls, "__hash__")
1475
+ ):
1476
+ hash = False
1477
+
1478
+ if hash is not True and hash is not False and hash is not None:
1479
+ # Can't use `hash in` because 1 == True for example.
1480
+ msg = "Invalid value for hash. Must be True, False, or None."
1481
+ raise TypeError(msg)
1482
+
1483
+ if hash is False or (hash is None and eq is False) or is_exc:
1484
+ # Don't do anything. Should fall back to __object__'s __hash__
1485
+ # which is by id.
1486
+ if cache_hash:
1487
+ msg = "Invalid value for cache_hash. To use hash caching, hashing must be either explicitly or implicitly enabled."
1488
+ raise TypeError(msg)
1489
+ elif hash is True or (
1490
+ hash is None and eq is True and is_frozen is True
1491
+ ):
1492
+ # Build a __hash__ if told so, or if it's safe.
1493
+ builder.add_hash()
1494
+ else:
1495
+ # Raise TypeError on attempts to hash.
1496
+ if cache_hash:
1497
+ msg = "Invalid value for cache_hash. To use hash caching, hashing must be either explicitly or implicitly enabled."
1498
+ raise TypeError(msg)
1499
+ builder.make_unhashable()
1500
+
1501
+ if _determine_whether_to_implement(
1502
+ cls, init, auto_detect, ("__init__",)
1503
+ ):
1504
+ builder.add_init()
1505
+ else:
1506
+ builder.add_attrs_init()
1507
+ if cache_hash:
1508
+ msg = "Invalid value for cache_hash. To use hash caching, init must be True."
1509
+ raise TypeError(msg)
1510
+
1511
+ if PY_3_13_PLUS and not _has_own_attribute(cls, "__replace__"):
1512
+ builder.add_replace()
1513
+
1514
+ if (
1515
+ PY_3_10_PLUS
1516
+ and match_args
1517
+ and not _has_own_attribute(cls, "__match_args__")
1518
+ ):
1519
+ builder.add_match_args()
1520
+
1521
+ return builder.build_class()
1522
+
1523
+ # maybe_cls's type depends on the usage of the decorator. It's a class
1524
+ # if it's used as `@attrs` but `None` if used as `@attrs()`.
1525
+ if maybe_cls is None:
1526
+ return wrap
1527
+
1528
+ return wrap(maybe_cls)
1529
+
1530
+
1531
+ _attrs = attrs
1532
+ """
1533
+ Internal alias so we can use it in functions that take an argument called
1534
+ *attrs*.
1535
+ """
1536
+
1537
+
1538
+ def _has_frozen_base_class(cls):
1539
+ """
1540
+ Check whether *cls* has a frozen ancestor by looking at its
1541
+ __setattr__.
1542
+ """
1543
+ return cls.__setattr__ is _frozen_setattrs
1544
+
1545
+
1546
+ def _generate_unique_filename(cls: type, func_name: str) -> str:
1547
+ """
1548
+ Create a "filename" suitable for a function being generated.
1549
+ """
1550
+ return (
1551
+ f"<attrs generated {func_name} {cls.__module__}."
1552
+ f"{getattr(cls, '__qualname__', cls.__name__)}>"
1553
+ )
1554
+
1555
+
1556
+ def _make_hash_script(
1557
+ cls: type, attrs: list[Attribute], frozen: bool, cache_hash: bool
1558
+ ) -> tuple[str, dict]:
1559
+ attrs = tuple(
1560
+ a for a in attrs if a.hash is True or (a.hash is None and a.eq is True)
1561
+ )
1562
+
1563
+ tab = " "
1564
+
1565
+ type_hash = hash(_generate_unique_filename(cls, "hash"))
1566
+ # If eq is custom generated, we need to include the functions in globs
1567
+ globs = {}
1568
+
1569
+ hash_def = "def __hash__(self"
1570
+ hash_func = "hash(("
1571
+ closing_braces = "))"
1572
+ if not cache_hash:
1573
+ hash_def += "):"
1574
+ else:
1575
+ hash_def += ", *"
1576
+
1577
+ hash_def += ", _cache_wrapper=__import__('attr._make')._make._CacheHashWrapper):"
1578
+ hash_func = "_cache_wrapper(" + hash_func
1579
+ closing_braces += ")"
1580
+
1581
+ method_lines = [hash_def]
1582
+
1583
+ def append_hash_computation_lines(prefix, indent):
1584
+ """
1585
+ Generate the code for actually computing the hash code.
1586
+ Below this will either be returned directly or used to compute
1587
+ a value which is then cached, depending on the value of cache_hash
1588
+ """
1589
+
1590
+ method_lines.extend(
1591
+ [
1592
+ indent + prefix + hash_func,
1593
+ indent + f" {type_hash},",
1594
+ ]
1595
+ )
1596
+
1597
+ for a in attrs:
1598
+ if a.eq_key:
1599
+ cmp_name = f"_{a.name}_key"
1600
+ globs[cmp_name] = a.eq_key
1601
+ method_lines.append(
1602
+ indent + f" {cmp_name}(self.{a.name}),"
1603
+ )
1604
+ else:
1605
+ method_lines.append(indent + f" self.{a.name},")
1606
+
1607
+ method_lines.append(indent + " " + closing_braces)
1608
+
1609
+ if cache_hash:
1610
+ method_lines.append(tab + f"if self.{_HASH_CACHE_FIELD} is None:")
1611
+ if frozen:
1612
+ append_hash_computation_lines(
1613
+ f"object.__setattr__(self, '{_HASH_CACHE_FIELD}', ", tab * 2
1614
+ )
1615
+ method_lines.append(tab * 2 + ")") # close __setattr__
1616
+ else:
1617
+ append_hash_computation_lines(
1618
+ f"self.{_HASH_CACHE_FIELD} = ", tab * 2
1619
+ )
1620
+ method_lines.append(tab + f"return self.{_HASH_CACHE_FIELD}")
1621
+ else:
1622
+ append_hash_computation_lines("return ", tab)
1623
+
1624
+ script = "\n".join(method_lines)
1625
+ return script, globs
1626
+
1627
+
1628
+ def _add_hash(cls: type, attrs: list[Attribute]):
1629
+ """
1630
+ Add a hash method to *cls*.
1631
+ """
1632
+ script, globs = _make_hash_script(
1633
+ cls, attrs, frozen=False, cache_hash=False
1634
+ )
1635
+ _compile_and_eval(
1636
+ script, globs, filename=_generate_unique_filename(cls, "__hash__")
1637
+ )
1638
+ cls.__hash__ = globs["__hash__"]
1639
+ return cls
1640
+
1641
+
1642
+ def __ne__(self, other):
1643
+ """
1644
+ Check equality and either forward a NotImplemented or
1645
+ return the result negated.
1646
+ """
1647
+ result = self.__eq__(other)
1648
+ if result is NotImplemented:
1649
+ return NotImplemented
1650
+
1651
+ return not result
1652
+
1653
+
1654
+ def _make_eq_script(attrs: list) -> tuple[str, dict]:
1655
+ """
1656
+ Create __eq__ method for *cls* with *attrs*.
1657
+ """
1658
+ attrs = [a for a in attrs if a.eq]
1659
+
1660
+ lines = [
1661
+ "def __eq__(self, other):",
1662
+ " if other.__class__ is not self.__class__:",
1663
+ " return NotImplemented",
1664
+ ]
1665
+
1666
+ globs = {}
1667
+ if attrs:
1668
+ lines.append(" return (")
1669
+ for a in attrs:
1670
+ if a.eq_key:
1671
+ cmp_name = f"_{a.name}_key"
1672
+ # Add the key function to the global namespace
1673
+ # of the evaluated function.
1674
+ globs[cmp_name] = a.eq_key
1675
+ lines.append(
1676
+ f" {cmp_name}(self.{a.name}) == {cmp_name}(other.{a.name})"
1677
+ )
1678
+ else:
1679
+ lines.append(f" self.{a.name} == other.{a.name}")
1680
+ if a is not attrs[-1]:
1681
+ lines[-1] = f"{lines[-1]} and"
1682
+ lines.append(" )")
1683
+ else:
1684
+ lines.append(" return True")
1685
+
1686
+ script = "\n".join(lines)
1687
+
1688
+ return script, globs
1689
+
1690
+
1691
+ def _make_order(cls, attrs):
1692
+ """
1693
+ Create ordering methods for *cls* with *attrs*.
1694
+ """
1695
+ attrs = [a for a in attrs if a.order]
1696
+
1697
+ def attrs_to_tuple(obj):
1698
+ """
1699
+ Save us some typing.
1700
+ """
1701
+ return tuple(
1702
+ key(value) if key else value
1703
+ for value, key in (
1704
+ (getattr(obj, a.name), a.order_key) for a in attrs
1705
+ )
1706
+ )
1707
+
1708
+ def __lt__(self, other):
1709
+ """
1710
+ Automatically created by attrs.
1711
+ """
1712
+ if other.__class__ is self.__class__:
1713
+ return attrs_to_tuple(self) < attrs_to_tuple(other)
1714
+
1715
+ return NotImplemented
1716
+
1717
+ def __le__(self, other):
1718
+ """
1719
+ Automatically created by attrs.
1720
+ """
1721
+ if other.__class__ is self.__class__:
1722
+ return attrs_to_tuple(self) <= attrs_to_tuple(other)
1723
+
1724
+ return NotImplemented
1725
+
1726
+ def __gt__(self, other):
1727
+ """
1728
+ Automatically created by attrs.
1729
+ """
1730
+ if other.__class__ is self.__class__:
1731
+ return attrs_to_tuple(self) > attrs_to_tuple(other)
1732
+
1733
+ return NotImplemented
1734
+
1735
+ def __ge__(self, other):
1736
+ """
1737
+ Automatically created by attrs.
1738
+ """
1739
+ if other.__class__ is self.__class__:
1740
+ return attrs_to_tuple(self) >= attrs_to_tuple(other)
1741
+
1742
+ return NotImplemented
1743
+
1744
+ return __lt__, __le__, __gt__, __ge__
1745
+
1746
+
1747
+ def _add_eq(cls, attrs=None):
1748
+ """
1749
+ Add equality methods to *cls* with *attrs*.
1750
+ """
1751
+ if attrs is None:
1752
+ attrs = cls.__attrs_attrs__
1753
+
1754
+ script, globs = _make_eq_script(attrs)
1755
+ _compile_and_eval(
1756
+ script, globs, filename=_generate_unique_filename(cls, "__eq__")
1757
+ )
1758
+ cls.__eq__ = globs["__eq__"]
1759
+ cls.__ne__ = __ne__
1760
+
1761
+ return cls
1762
+
1763
+
1764
+ def _make_repr_script(attrs, ns) -> tuple[str, dict]:
1765
+ """
1766
+ Create the source and globs for a __repr__ and return it.
1767
+ """
1768
+ # Figure out which attributes to include, and which function to use to
1769
+ # format them. The a.repr value can be either bool or a custom
1770
+ # callable.
1771
+ attr_names_with_reprs = tuple(
1772
+ (a.name, (repr if a.repr is True else a.repr), a.init)
1773
+ for a in attrs
1774
+ if a.repr is not False
1775
+ )
1776
+ globs = {
1777
+ name + "_repr": r for name, r, _ in attr_names_with_reprs if r != repr
1778
+ }
1779
+ globs["_compat"] = _compat
1780
+ globs["AttributeError"] = AttributeError
1781
+ globs["NOTHING"] = NOTHING
1782
+ attribute_fragments = []
1783
+ for name, r, i in attr_names_with_reprs:
1784
+ accessor = (
1785
+ "self." + name if i else 'getattr(self, "' + name + '", NOTHING)'
1786
+ )
1787
+ fragment = (
1788
+ "%s={%s!r}" % (name, accessor)
1789
+ if r == repr
1790
+ else "%s={%s_repr(%s)}" % (name, name, accessor)
1791
+ )
1792
+ attribute_fragments.append(fragment)
1793
+ repr_fragment = ", ".join(attribute_fragments)
1794
+
1795
+ if ns is None:
1796
+ cls_name_fragment = '{self.__class__.__qualname__.rsplit(">.", 1)[-1]}'
1797
+ else:
1798
+ cls_name_fragment = ns + ".{self.__class__.__name__}"
1799
+
1800
+ lines = [
1801
+ "def __repr__(self):",
1802
+ " try:",
1803
+ " already_repring = _compat.repr_context.already_repring",
1804
+ " except AttributeError:",
1805
+ " already_repring = {id(self),}",
1806
+ " _compat.repr_context.already_repring = already_repring",
1807
+ " else:",
1808
+ " if id(self) in already_repring:",
1809
+ " return '...'",
1810
+ " else:",
1811
+ " already_repring.add(id(self))",
1812
+ " try:",
1813
+ f" return f'{cls_name_fragment}({repr_fragment})'",
1814
+ " finally:",
1815
+ " already_repring.remove(id(self))",
1816
+ ]
1817
+
1818
+ return "\n".join(lines), globs
1819
+
1820
+
1821
+ def _add_repr(cls, ns=None, attrs=None):
1822
+ """
1823
+ Add a repr method to *cls*.
1824
+ """
1825
+ if attrs is None:
1826
+ attrs = cls.__attrs_attrs__
1827
+
1828
+ script, globs = _make_repr_script(attrs, ns)
1829
+ _compile_and_eval(
1830
+ script, globs, filename=_generate_unique_filename(cls, "__repr__")
1831
+ )
1832
+ cls.__repr__ = globs["__repr__"]
1833
+ return cls
1834
+
1835
+
1836
+ def fields(cls):
1837
+ """
1838
+ Return the tuple of *attrs* attributes for a class.
1839
+
1840
+ The tuple also allows accessing the fields by their names (see below for
1841
+ examples).
1842
+
1843
+ Args:
1844
+ cls (type): Class to introspect.
1845
+
1846
+ Raises:
1847
+ TypeError: If *cls* is not a class.
1848
+
1849
+ attrs.exceptions.NotAnAttrsClassError:
1850
+ If *cls* is not an *attrs* class.
1851
+
1852
+ Returns:
1853
+ tuple (with name accessors) of `attrs.Attribute`
1854
+
1855
+ .. versionchanged:: 16.2.0 Returned tuple allows accessing the fields
1856
+ by name.
1857
+ .. versionchanged:: 23.1.0 Add support for generic classes.
1858
+ """
1859
+ generic_base = get_generic_base(cls)
1860
+
1861
+ if generic_base is None and not isinstance(cls, type):
1862
+ msg = "Passed object must be a class."
1863
+ raise TypeError(msg)
1864
+
1865
+ attrs = getattr(cls, "__attrs_attrs__", None)
1866
+
1867
+ if attrs is None:
1868
+ if generic_base is not None:
1869
+ attrs = getattr(generic_base, "__attrs_attrs__", None)
1870
+ if attrs is not None:
1871
+ # Even though this is global state, stick it on here to speed
1872
+ # it up. We rely on `cls` being cached for this to be
1873
+ # efficient.
1874
+ cls.__attrs_attrs__ = attrs
1875
+ return attrs
1876
+ msg = f"{cls!r} is not an attrs-decorated class."
1877
+ raise NotAnAttrsClassError(msg)
1878
+
1879
+ return attrs
1880
+
1881
+
1882
+ def fields_dict(cls):
1883
+ """
1884
+ Return an ordered dictionary of *attrs* attributes for a class, whose keys
1885
+ are the attribute names.
1886
+
1887
+ Args:
1888
+ cls (type): Class to introspect.
1889
+
1890
+ Raises:
1891
+ TypeError: If *cls* is not a class.
1892
+
1893
+ attrs.exceptions.NotAnAttrsClassError:
1894
+ If *cls* is not an *attrs* class.
1895
+
1896
+ Returns:
1897
+ dict[str, attrs.Attribute]: Dict of attribute name to definition
1898
+
1899
+ .. versionadded:: 18.1.0
1900
+ """
1901
+ if not isinstance(cls, type):
1902
+ msg = "Passed object must be a class."
1903
+ raise TypeError(msg)
1904
+ attrs = getattr(cls, "__attrs_attrs__", None)
1905
+ if attrs is None:
1906
+ msg = f"{cls!r} is not an attrs-decorated class."
1907
+ raise NotAnAttrsClassError(msg)
1908
+ return {a.name: a for a in attrs}
1909
+
1910
+
1911
+ def validate(inst):
1912
+ """
1913
+ Validate all attributes on *inst* that have a validator.
1914
+
1915
+ Leaves all exceptions through.
1916
+
1917
+ Args:
1918
+ inst: Instance of a class with *attrs* attributes.
1919
+ """
1920
+ if _config._run_validators is False:
1921
+ return
1922
+
1923
+ for a in fields(inst.__class__):
1924
+ v = a.validator
1925
+ if v is not None:
1926
+ v(inst, a, getattr(inst, a.name))
1927
+
1928
+
1929
+ def _is_slot_attr(a_name, base_attr_map):
1930
+ """
1931
+ Check if the attribute name comes from a slot class.
1932
+ """
1933
+ cls = base_attr_map.get(a_name)
1934
+ return cls and "__slots__" in cls.__dict__
1935
+
1936
+
1937
+ def _make_init_script(
1938
+ cls,
1939
+ attrs,
1940
+ pre_init,
1941
+ pre_init_has_args,
1942
+ post_init,
1943
+ frozen,
1944
+ slots,
1945
+ cache_hash,
1946
+ base_attr_map,
1947
+ is_exc,
1948
+ cls_on_setattr,
1949
+ attrs_init,
1950
+ ) -> tuple[str, dict, dict]:
1951
+ has_cls_on_setattr = (
1952
+ cls_on_setattr is not None and cls_on_setattr is not setters.NO_OP
1953
+ )
1954
+
1955
+ if frozen and has_cls_on_setattr:
1956
+ msg = "Frozen classes can't use on_setattr."
1957
+ raise ValueError(msg)
1958
+
1959
+ needs_cached_setattr = cache_hash or frozen
1960
+ filtered_attrs = []
1961
+ attr_dict = {}
1962
+ for a in attrs:
1963
+ if not a.init and a.default is NOTHING:
1964
+ continue
1965
+
1966
+ filtered_attrs.append(a)
1967
+ attr_dict[a.name] = a
1968
+
1969
+ if a.on_setattr is not None:
1970
+ if frozen is True:
1971
+ msg = "Frozen classes can't use on_setattr."
1972
+ raise ValueError(msg)
1973
+
1974
+ needs_cached_setattr = True
1975
+ elif has_cls_on_setattr and a.on_setattr is not setters.NO_OP:
1976
+ needs_cached_setattr = True
1977
+
1978
+ script, globs, annotations = _attrs_to_init_script(
1979
+ filtered_attrs,
1980
+ frozen,
1981
+ slots,
1982
+ pre_init,
1983
+ pre_init_has_args,
1984
+ post_init,
1985
+ cache_hash,
1986
+ base_attr_map,
1987
+ is_exc,
1988
+ needs_cached_setattr,
1989
+ has_cls_on_setattr,
1990
+ "__attrs_init__" if attrs_init else "__init__",
1991
+ )
1992
+ if cls.__module__ in sys.modules:
1993
+ # This makes typing.get_type_hints(CLS.__init__) resolve string types.
1994
+ globs.update(sys.modules[cls.__module__].__dict__)
1995
+
1996
+ globs.update({"NOTHING": NOTHING, "attr_dict": attr_dict})
1997
+
1998
+ if needs_cached_setattr:
1999
+ # Save the lookup overhead in __init__ if we need to circumvent
2000
+ # setattr hooks.
2001
+ globs["_cached_setattr_get"] = _OBJ_SETATTR.__get__
2002
+
2003
+ return script, globs, annotations
2004
+
2005
+
2006
+ def _setattr(attr_name: str, value_var: str, has_on_setattr: bool) -> str:
2007
+ """
2008
+ Use the cached object.setattr to set *attr_name* to *value_var*.
2009
+ """
2010
+ return f"_setattr('{attr_name}', {value_var})"
2011
+
2012
+
2013
+ def _setattr_with_converter(
2014
+ attr_name: str, value_var: str, has_on_setattr: bool, converter: Converter
2015
+ ) -> str:
2016
+ """
2017
+ Use the cached object.setattr to set *attr_name* to *value_var*, but run
2018
+ its converter first.
2019
+ """
2020
+ return f"_setattr('{attr_name}', {converter._fmt_converter_call(attr_name, value_var)})"
2021
+
2022
+
2023
+ def _assign(attr_name: str, value: str, has_on_setattr: bool) -> str:
2024
+ """
2025
+ Unless *attr_name* has an on_setattr hook, use normal assignment. Otherwise
2026
+ relegate to _setattr.
2027
+ """
2028
+ if has_on_setattr:
2029
+ return _setattr(attr_name, value, True)
2030
+
2031
+ return f"self.{attr_name} = {value}"
2032
+
2033
+
2034
+ def _assign_with_converter(
2035
+ attr_name: str, value_var: str, has_on_setattr: bool, converter: Converter
2036
+ ) -> str:
2037
+ """
2038
+ Unless *attr_name* has an on_setattr hook, use normal assignment after
2039
+ conversion. Otherwise relegate to _setattr_with_converter.
2040
+ """
2041
+ if has_on_setattr:
2042
+ return _setattr_with_converter(attr_name, value_var, True, converter)
2043
+
2044
+ return f"self.{attr_name} = {converter._fmt_converter_call(attr_name, value_var)}"
2045
+
2046
+
2047
+ def _determine_setters(
2048
+ frozen: bool, slots: bool, base_attr_map: dict[str, type]
2049
+ ):
2050
+ """
2051
+ Determine the correct setter functions based on whether a class is frozen
2052
+ and/or slotted.
2053
+ """
2054
+ if frozen is True:
2055
+ if slots is True:
2056
+ return (), _setattr, _setattr_with_converter
2057
+
2058
+ # Dict frozen classes assign directly to __dict__.
2059
+ # But only if the attribute doesn't come from an ancestor slot
2060
+ # class.
2061
+ # Note _inst_dict will be used again below if cache_hash is True
2062
+
2063
+ def fmt_setter(
2064
+ attr_name: str, value_var: str, has_on_setattr: bool
2065
+ ) -> str:
2066
+ if _is_slot_attr(attr_name, base_attr_map):
2067
+ return _setattr(attr_name, value_var, has_on_setattr)
2068
+
2069
+ return f"_inst_dict['{attr_name}'] = {value_var}"
2070
+
2071
+ def fmt_setter_with_converter(
2072
+ attr_name: str,
2073
+ value_var: str,
2074
+ has_on_setattr: bool,
2075
+ converter: Converter,
2076
+ ) -> str:
2077
+ if has_on_setattr or _is_slot_attr(attr_name, base_attr_map):
2078
+ return _setattr_with_converter(
2079
+ attr_name, value_var, has_on_setattr, converter
2080
+ )
2081
+
2082
+ return f"_inst_dict['{attr_name}'] = {converter._fmt_converter_call(attr_name, value_var)}"
2083
+
2084
+ return (
2085
+ ("_inst_dict = self.__dict__",),
2086
+ fmt_setter,
2087
+ fmt_setter_with_converter,
2088
+ )
2089
+
2090
+ # Not frozen -- we can just assign directly.
2091
+ return (), _assign, _assign_with_converter
2092
+
2093
+
2094
+ def _attrs_to_init_script(
2095
+ attrs: list[Attribute],
2096
+ is_frozen: bool,
2097
+ is_slotted: bool,
2098
+ call_pre_init: bool,
2099
+ pre_init_has_args: bool,
2100
+ call_post_init: bool,
2101
+ does_cache_hash: bool,
2102
+ base_attr_map: dict[str, type],
2103
+ is_exc: bool,
2104
+ needs_cached_setattr: bool,
2105
+ has_cls_on_setattr: bool,
2106
+ method_name: str,
2107
+ ) -> tuple[str, dict, dict]:
2108
+ """
2109
+ Return a script of an initializer for *attrs*, a dict of globals, and
2110
+ annotations for the initializer.
2111
+
2112
+ The globals are required by the generated script.
2113
+ """
2114
+ lines = ["self.__attrs_pre_init__()"] if call_pre_init else []
2115
+
2116
+ if needs_cached_setattr:
2117
+ lines.append(
2118
+ # Circumvent the __setattr__ descriptor to save one lookup per
2119
+ # assignment. Note _setattr will be used again below if
2120
+ # does_cache_hash is True.
2121
+ "_setattr = _cached_setattr_get(self)"
2122
+ )
2123
+
2124
+ extra_lines, fmt_setter, fmt_setter_with_converter = _determine_setters(
2125
+ is_frozen, is_slotted, base_attr_map
2126
+ )
2127
+ lines.extend(extra_lines)
2128
+
2129
+ args = []
2130
+ kw_only_args = []
2131
+ attrs_to_validate = []
2132
+
2133
+ # This is a dictionary of names to validator and converter callables.
2134
+ # Injecting this into __init__ globals lets us avoid lookups.
2135
+ names_for_globals = {}
2136
+ annotations = {"return": None}
2137
+
2138
+ for a in attrs:
2139
+ if a.validator:
2140
+ attrs_to_validate.append(a)
2141
+
2142
+ attr_name = a.name
2143
+ has_on_setattr = a.on_setattr is not None or (
2144
+ a.on_setattr is not setters.NO_OP and has_cls_on_setattr
2145
+ )
2146
+ # a.alias is set to maybe-mangled attr_name in _ClassBuilder if not
2147
+ # explicitly provided
2148
+ arg_name = a.alias
2149
+
2150
+ has_factory = isinstance(a.default, Factory)
2151
+ maybe_self = "self" if has_factory and a.default.takes_self else ""
2152
+
2153
+ if a.converter is not None and not isinstance(a.converter, Converter):
2154
+ converter = Converter(a.converter)
2155
+ else:
2156
+ converter = a.converter
2157
+
2158
+ if a.init is False:
2159
+ if has_factory:
2160
+ init_factory_name = _INIT_FACTORY_PAT % (a.name,)
2161
+ if converter is not None:
2162
+ lines.append(
2163
+ fmt_setter_with_converter(
2164
+ attr_name,
2165
+ init_factory_name + f"({maybe_self})",
2166
+ has_on_setattr,
2167
+ converter,
2168
+ )
2169
+ )
2170
+ names_for_globals[converter._get_global_name(a.name)] = (
2171
+ converter.converter
2172
+ )
2173
+ else:
2174
+ lines.append(
2175
+ fmt_setter(
2176
+ attr_name,
2177
+ init_factory_name + f"({maybe_self})",
2178
+ has_on_setattr,
2179
+ )
2180
+ )
2181
+ names_for_globals[init_factory_name] = a.default.factory
2182
+ elif converter is not None:
2183
+ lines.append(
2184
+ fmt_setter_with_converter(
2185
+ attr_name,
2186
+ f"attr_dict['{attr_name}'].default",
2187
+ has_on_setattr,
2188
+ converter,
2189
+ )
2190
+ )
2191
+ names_for_globals[converter._get_global_name(a.name)] = (
2192
+ converter.converter
2193
+ )
2194
+ else:
2195
+ lines.append(
2196
+ fmt_setter(
2197
+ attr_name,
2198
+ f"attr_dict['{attr_name}'].default",
2199
+ has_on_setattr,
2200
+ )
2201
+ )
2202
+ elif a.default is not NOTHING and not has_factory:
2203
+ arg = f"{arg_name}=attr_dict['{attr_name}'].default"
2204
+ if a.kw_only:
2205
+ kw_only_args.append(arg)
2206
+ else:
2207
+ args.append(arg)
2208
+
2209
+ if converter is not None:
2210
+ lines.append(
2211
+ fmt_setter_with_converter(
2212
+ attr_name, arg_name, has_on_setattr, converter
2213
+ )
2214
+ )
2215
+ names_for_globals[converter._get_global_name(a.name)] = (
2216
+ converter.converter
2217
+ )
2218
+ else:
2219
+ lines.append(fmt_setter(attr_name, arg_name, has_on_setattr))
2220
+
2221
+ elif has_factory:
2222
+ arg = f"{arg_name}=NOTHING"
2223
+ if a.kw_only:
2224
+ kw_only_args.append(arg)
2225
+ else:
2226
+ args.append(arg)
2227
+ lines.append(f"if {arg_name} is not NOTHING:")
2228
+
2229
+ init_factory_name = _INIT_FACTORY_PAT % (a.name,)
2230
+ if converter is not None:
2231
+ lines.append(
2232
+ " "
2233
+ + fmt_setter_with_converter(
2234
+ attr_name, arg_name, has_on_setattr, converter
2235
+ )
2236
+ )
2237
+ lines.append("else:")
2238
+ lines.append(
2239
+ " "
2240
+ + fmt_setter_with_converter(
2241
+ attr_name,
2242
+ init_factory_name + "(" + maybe_self + ")",
2243
+ has_on_setattr,
2244
+ converter,
2245
+ )
2246
+ )
2247
+ names_for_globals[converter._get_global_name(a.name)] = (
2248
+ converter.converter
2249
+ )
2250
+ else:
2251
+ lines.append(
2252
+ " " + fmt_setter(attr_name, arg_name, has_on_setattr)
2253
+ )
2254
+ lines.append("else:")
2255
+ lines.append(
2256
+ " "
2257
+ + fmt_setter(
2258
+ attr_name,
2259
+ init_factory_name + "(" + maybe_self + ")",
2260
+ has_on_setattr,
2261
+ )
2262
+ )
2263
+ names_for_globals[init_factory_name] = a.default.factory
2264
+ else:
2265
+ if a.kw_only:
2266
+ kw_only_args.append(arg_name)
2267
+ else:
2268
+ args.append(arg_name)
2269
+
2270
+ if converter is not None:
2271
+ lines.append(
2272
+ fmt_setter_with_converter(
2273
+ attr_name, arg_name, has_on_setattr, converter
2274
+ )
2275
+ )
2276
+ names_for_globals[converter._get_global_name(a.name)] = (
2277
+ converter.converter
2278
+ )
2279
+ else:
2280
+ lines.append(fmt_setter(attr_name, arg_name, has_on_setattr))
2281
+
2282
+ if a.init is True:
2283
+ if a.type is not None and converter is None:
2284
+ annotations[arg_name] = a.type
2285
+ elif converter is not None and converter._first_param_type:
2286
+ # Use the type from the converter if present.
2287
+ annotations[arg_name] = converter._first_param_type
2288
+
2289
+ if attrs_to_validate: # we can skip this if there are no validators.
2290
+ names_for_globals["_config"] = _config
2291
+ lines.append("if _config._run_validators is True:")
2292
+ for a in attrs_to_validate:
2293
+ val_name = "__attr_validator_" + a.name
2294
+ attr_name = "__attr_" + a.name
2295
+ lines.append(f" {val_name}(self, {attr_name}, self.{a.name})")
2296
+ names_for_globals[val_name] = a.validator
2297
+ names_for_globals[attr_name] = a
2298
+
2299
+ if call_post_init:
2300
+ lines.append("self.__attrs_post_init__()")
2301
+
2302
+ # Because this is set only after __attrs_post_init__ is called, a crash
2303
+ # will result if post-init tries to access the hash code. This seemed
2304
+ # preferable to setting this beforehand, in which case alteration to field
2305
+ # values during post-init combined with post-init accessing the hash code
2306
+ # would result in silent bugs.
2307
+ if does_cache_hash:
2308
+ if is_frozen:
2309
+ if is_slotted:
2310
+ init_hash_cache = f"_setattr('{_HASH_CACHE_FIELD}', None)"
2311
+ else:
2312
+ init_hash_cache = f"_inst_dict['{_HASH_CACHE_FIELD}'] = None"
2313
+ else:
2314
+ init_hash_cache = f"self.{_HASH_CACHE_FIELD} = None"
2315
+ lines.append(init_hash_cache)
2316
+
2317
+ # For exceptions we rely on BaseException.__init__ for proper
2318
+ # initialization.
2319
+ if is_exc:
2320
+ vals = ",".join(f"self.{a.name}" for a in attrs if a.init)
2321
+
2322
+ lines.append(f"BaseException.__init__(self, {vals})")
2323
+
2324
+ args = ", ".join(args)
2325
+ pre_init_args = args
2326
+ if kw_only_args:
2327
+ # leading comma & kw_only args
2328
+ args += f"{', ' if args else ''}*, {', '.join(kw_only_args)}"
2329
+ pre_init_kw_only_args = ", ".join(
2330
+ [
2331
+ f"{kw_arg_name}={kw_arg_name}"
2332
+ # We need to remove the defaults from the kw_only_args.
2333
+ for kw_arg_name in (kwa.split("=")[0] for kwa in kw_only_args)
2334
+ ]
2335
+ )
2336
+ pre_init_args += ", " if pre_init_args else ""
2337
+ pre_init_args += pre_init_kw_only_args
2338
+
2339
+ if call_pre_init and pre_init_has_args:
2340
+ # If pre init method has arguments, pass same arguments as `__init__`.
2341
+ lines[0] = f"self.__attrs_pre_init__({pre_init_args})"
2342
+
2343
+ # Python <3.12 doesn't allow backslashes in f-strings.
2344
+ NL = "\n "
2345
+ return (
2346
+ f"""def {method_name}(self, {args}):
2347
+ {NL.join(lines) if lines else "pass"}
2348
+ """,
2349
+ names_for_globals,
2350
+ annotations,
2351
+ )
2352
+
2353
+
2354
+ def _default_init_alias_for(name: str) -> str:
2355
+ """
2356
+ The default __init__ parameter name for a field.
2357
+
2358
+ This performs private-name adjustment via leading-unscore stripping,
2359
+ and is the default value of Attribute.alias if not provided.
2360
+ """
2361
+
2362
+ return name.lstrip("_")
2363
+
2364
+
2365
+ class Attribute:
2366
+ """
2367
+ *Read-only* representation of an attribute.
2368
+
2369
+ .. warning::
2370
+
2371
+ You should never instantiate this class yourself.
2372
+
2373
+ The class has *all* arguments of `attr.ib` (except for ``factory`` which is
2374
+ only syntactic sugar for ``default=Factory(...)`` plus the following:
2375
+
2376
+ - ``name`` (`str`): The name of the attribute.
2377
+ - ``alias`` (`str`): The __init__ parameter name of the attribute, after
2378
+ any explicit overrides and default private-attribute-name handling.
2379
+ - ``inherited`` (`bool`): Whether or not that attribute has been inherited
2380
+ from a base class.
2381
+ - ``eq_key`` and ``order_key`` (`typing.Callable` or `None`): The
2382
+ callables that are used for comparing and ordering objects by this
2383
+ attribute, respectively. These are set by passing a callable to
2384
+ `attr.ib`'s ``eq``, ``order``, or ``cmp`` arguments. See also
2385
+ :ref:`comparison customization <custom-comparison>`.
2386
+
2387
+ Instances of this class are frequently used for introspection purposes
2388
+ like:
2389
+
2390
+ - `fields` returns a tuple of them.
2391
+ - Validators get them passed as the first argument.
2392
+ - The :ref:`field transformer <transform-fields>` hook receives a list of
2393
+ them.
2394
+ - The ``alias`` property exposes the __init__ parameter name of the field,
2395
+ with any overrides and default private-attribute handling applied.
2396
+
2397
+
2398
+ .. versionadded:: 20.1.0 *inherited*
2399
+ .. versionadded:: 20.1.0 *on_setattr*
2400
+ .. versionchanged:: 20.2.0 *inherited* is not taken into account for
2401
+ equality checks and hashing anymore.
2402
+ .. versionadded:: 21.1.0 *eq_key* and *order_key*
2403
+ .. versionadded:: 22.2.0 *alias*
2404
+
2405
+ For the full version history of the fields, see `attr.ib`.
2406
+ """
2407
+
2408
+ # These slots must NOT be reordered because we use them later for
2409
+ # instantiation.
2410
+ __slots__ = ( # noqa: RUF023
2411
+ "name",
2412
+ "default",
2413
+ "validator",
2414
+ "repr",
2415
+ "eq",
2416
+ "eq_key",
2417
+ "order",
2418
+ "order_key",
2419
+ "hash",
2420
+ "init",
2421
+ "metadata",
2422
+ "type",
2423
+ "converter",
2424
+ "kw_only",
2425
+ "inherited",
2426
+ "on_setattr",
2427
+ "alias",
2428
+ )
2429
+
2430
+ def __init__(
2431
+ self,
2432
+ name,
2433
+ default,
2434
+ validator,
2435
+ repr,
2436
+ cmp, # XXX: unused, remove along with other cmp code.
2437
+ hash,
2438
+ init,
2439
+ inherited,
2440
+ metadata=None,
2441
+ type=None,
2442
+ converter=None,
2443
+ kw_only=False,
2444
+ eq=None,
2445
+ eq_key=None,
2446
+ order=None,
2447
+ order_key=None,
2448
+ on_setattr=None,
2449
+ alias=None,
2450
+ ):
2451
+ eq, eq_key, order, order_key = _determine_attrib_eq_order(
2452
+ cmp, eq_key or eq, order_key or order, True
2453
+ )
2454
+
2455
+ # Cache this descriptor here to speed things up later.
2456
+ bound_setattr = _OBJ_SETATTR.__get__(self)
2457
+
2458
+ # Despite the big red warning, people *do* instantiate `Attribute`
2459
+ # themselves.
2460
+ bound_setattr("name", name)
2461
+ bound_setattr("default", default)
2462
+ bound_setattr("validator", validator)
2463
+ bound_setattr("repr", repr)
2464
+ bound_setattr("eq", eq)
2465
+ bound_setattr("eq_key", eq_key)
2466
+ bound_setattr("order", order)
2467
+ bound_setattr("order_key", order_key)
2468
+ bound_setattr("hash", hash)
2469
+ bound_setattr("init", init)
2470
+ bound_setattr("converter", converter)
2471
+ bound_setattr(
2472
+ "metadata",
2473
+ (
2474
+ types.MappingProxyType(dict(metadata)) # Shallow copy
2475
+ if metadata
2476
+ else _EMPTY_METADATA_SINGLETON
2477
+ ),
2478
+ )
2479
+ bound_setattr("type", type)
2480
+ bound_setattr("kw_only", kw_only)
2481
+ bound_setattr("inherited", inherited)
2482
+ bound_setattr("on_setattr", on_setattr)
2483
+ bound_setattr("alias", alias)
2484
+
2485
+ def __setattr__(self, name, value):
2486
+ raise FrozenInstanceError
2487
+
2488
+ @classmethod
2489
+ def from_counting_attr(cls, name: str, ca: _CountingAttr, type=None):
2490
+ # type holds the annotated value. deal with conflicts:
2491
+ if type is None:
2492
+ type = ca.type
2493
+ elif ca.type is not None:
2494
+ msg = f"Type annotation and type argument cannot both be present for '{name}'."
2495
+ raise ValueError(msg)
2496
+ return cls(
2497
+ name,
2498
+ ca._default,
2499
+ ca._validator,
2500
+ ca.repr,
2501
+ None,
2502
+ ca.hash,
2503
+ ca.init,
2504
+ False,
2505
+ ca.metadata,
2506
+ type,
2507
+ ca.converter,
2508
+ ca.kw_only,
2509
+ ca.eq,
2510
+ ca.eq_key,
2511
+ ca.order,
2512
+ ca.order_key,
2513
+ ca.on_setattr,
2514
+ ca.alias,
2515
+ )
2516
+
2517
+ # Don't use attrs.evolve since fields(Attribute) doesn't work
2518
+ def evolve(self, **changes):
2519
+ """
2520
+ Copy *self* and apply *changes*.
2521
+
2522
+ This works similarly to `attrs.evolve` but that function does not work
2523
+ with :class:`attrs.Attribute`.
2524
+
2525
+ It is mainly meant to be used for `transform-fields`.
2526
+
2527
+ .. versionadded:: 20.3.0
2528
+ """
2529
+ new = copy.copy(self)
2530
+
2531
+ new._setattrs(changes.items())
2532
+
2533
+ return new
2534
+
2535
+ # Don't use _add_pickle since fields(Attribute) doesn't work
2536
+ def __getstate__(self):
2537
+ """
2538
+ Play nice with pickle.
2539
+ """
2540
+ return tuple(
2541
+ getattr(self, name) if name != "metadata" else dict(self.metadata)
2542
+ for name in self.__slots__
2543
+ )
2544
+
2545
+ def __setstate__(self, state):
2546
+ """
2547
+ Play nice with pickle.
2548
+ """
2549
+ self._setattrs(zip(self.__slots__, state))
2550
+
2551
+ def _setattrs(self, name_values_pairs):
2552
+ bound_setattr = _OBJ_SETATTR.__get__(self)
2553
+ for name, value in name_values_pairs:
2554
+ if name != "metadata":
2555
+ bound_setattr(name, value)
2556
+ else:
2557
+ bound_setattr(
2558
+ name,
2559
+ (
2560
+ types.MappingProxyType(dict(value))
2561
+ if value
2562
+ else _EMPTY_METADATA_SINGLETON
2563
+ ),
2564
+ )
2565
+
2566
+
2567
+ _a = [
2568
+ Attribute(
2569
+ name=name,
2570
+ default=NOTHING,
2571
+ validator=None,
2572
+ repr=True,
2573
+ cmp=None,
2574
+ eq=True,
2575
+ order=False,
2576
+ hash=(name != "metadata"),
2577
+ init=True,
2578
+ inherited=False,
2579
+ alias=_default_init_alias_for(name),
2580
+ )
2581
+ for name in Attribute.__slots__
2582
+ ]
2583
+
2584
+ Attribute = _add_hash(
2585
+ _add_eq(
2586
+ _add_repr(Attribute, attrs=_a),
2587
+ attrs=[a for a in _a if a.name != "inherited"],
2588
+ ),
2589
+ attrs=[a for a in _a if a.hash and a.name != "inherited"],
2590
+ )
2591
+
2592
+
2593
+ class _CountingAttr:
2594
+ """
2595
+ Intermediate representation of attributes that uses a counter to preserve
2596
+ the order in which the attributes have been defined.
2597
+
2598
+ *Internal* data structure of the attrs library. Running into is most
2599
+ likely the result of a bug like a forgotten `@attr.s` decorator.
2600
+ """
2601
+
2602
+ __slots__ = (
2603
+ "_default",
2604
+ "_validator",
2605
+ "alias",
2606
+ "converter",
2607
+ "counter",
2608
+ "eq",
2609
+ "eq_key",
2610
+ "hash",
2611
+ "init",
2612
+ "kw_only",
2613
+ "metadata",
2614
+ "on_setattr",
2615
+ "order",
2616
+ "order_key",
2617
+ "repr",
2618
+ "type",
2619
+ )
2620
+ __attrs_attrs__ = (
2621
+ *tuple(
2622
+ Attribute(
2623
+ name=name,
2624
+ alias=_default_init_alias_for(name),
2625
+ default=NOTHING,
2626
+ validator=None,
2627
+ repr=True,
2628
+ cmp=None,
2629
+ hash=True,
2630
+ init=True,
2631
+ kw_only=False,
2632
+ eq=True,
2633
+ eq_key=None,
2634
+ order=False,
2635
+ order_key=None,
2636
+ inherited=False,
2637
+ on_setattr=None,
2638
+ )
2639
+ for name in (
2640
+ "counter",
2641
+ "_default",
2642
+ "repr",
2643
+ "eq",
2644
+ "order",
2645
+ "hash",
2646
+ "init",
2647
+ "on_setattr",
2648
+ "alias",
2649
+ )
2650
+ ),
2651
+ Attribute(
2652
+ name="metadata",
2653
+ alias="metadata",
2654
+ default=None,
2655
+ validator=None,
2656
+ repr=True,
2657
+ cmp=None,
2658
+ hash=False,
2659
+ init=True,
2660
+ kw_only=False,
2661
+ eq=True,
2662
+ eq_key=None,
2663
+ order=False,
2664
+ order_key=None,
2665
+ inherited=False,
2666
+ on_setattr=None,
2667
+ ),
2668
+ )
2669
+ cls_counter = 0
2670
+
2671
+ def __init__(
2672
+ self,
2673
+ default,
2674
+ validator,
2675
+ repr,
2676
+ cmp,
2677
+ hash,
2678
+ init,
2679
+ converter,
2680
+ metadata,
2681
+ type,
2682
+ kw_only,
2683
+ eq,
2684
+ eq_key,
2685
+ order,
2686
+ order_key,
2687
+ on_setattr,
2688
+ alias,
2689
+ ):
2690
+ _CountingAttr.cls_counter += 1
2691
+ self.counter = _CountingAttr.cls_counter
2692
+ self._default = default
2693
+ self._validator = validator
2694
+ self.converter = converter
2695
+ self.repr = repr
2696
+ self.eq = eq
2697
+ self.eq_key = eq_key
2698
+ self.order = order
2699
+ self.order_key = order_key
2700
+ self.hash = hash
2701
+ self.init = init
2702
+ self.metadata = metadata
2703
+ self.type = type
2704
+ self.kw_only = kw_only
2705
+ self.on_setattr = on_setattr
2706
+ self.alias = alias
2707
+
2708
+ def validator(self, meth):
2709
+ """
2710
+ Decorator that adds *meth* to the list of validators.
2711
+
2712
+ Returns *meth* unchanged.
2713
+
2714
+ .. versionadded:: 17.1.0
2715
+ """
2716
+ if self._validator is None:
2717
+ self._validator = meth
2718
+ else:
2719
+ self._validator = and_(self._validator, meth)
2720
+ return meth
2721
+
2722
+ def default(self, meth):
2723
+ """
2724
+ Decorator that allows to set the default for an attribute.
2725
+
2726
+ Returns *meth* unchanged.
2727
+
2728
+ Raises:
2729
+ DefaultAlreadySetError: If default has been set before.
2730
+
2731
+ .. versionadded:: 17.1.0
2732
+ """
2733
+ if self._default is not NOTHING:
2734
+ raise DefaultAlreadySetError
2735
+
2736
+ self._default = Factory(meth, takes_self=True)
2737
+
2738
+ return meth
2739
+
2740
+
2741
+ _CountingAttr = _add_eq(_add_repr(_CountingAttr))
2742
+
2743
+
2744
+ class Factory:
2745
+ """
2746
+ Stores a factory callable.
2747
+
2748
+ If passed as the default value to `attrs.field`, the factory is used to
2749
+ generate a new value.
2750
+
2751
+ Args:
2752
+ factory (typing.Callable):
2753
+ A callable that takes either none or exactly one mandatory
2754
+ positional argument depending on *takes_self*.
2755
+
2756
+ takes_self (bool):
2757
+ Pass the partially initialized instance that is being initialized
2758
+ as a positional argument.
2759
+
2760
+ .. versionadded:: 17.1.0 *takes_self*
2761
+ """
2762
+
2763
+ __slots__ = ("factory", "takes_self")
2764
+
2765
+ def __init__(self, factory, takes_self=False):
2766
+ self.factory = factory
2767
+ self.takes_self = takes_self
2768
+
2769
+ def __getstate__(self):
2770
+ """
2771
+ Play nice with pickle.
2772
+ """
2773
+ return tuple(getattr(self, name) for name in self.__slots__)
2774
+
2775
+ def __setstate__(self, state):
2776
+ """
2777
+ Play nice with pickle.
2778
+ """
2779
+ for name, value in zip(self.__slots__, state):
2780
+ setattr(self, name, value)
2781
+
2782
+
2783
+ _f = [
2784
+ Attribute(
2785
+ name=name,
2786
+ default=NOTHING,
2787
+ validator=None,
2788
+ repr=True,
2789
+ cmp=None,
2790
+ eq=True,
2791
+ order=False,
2792
+ hash=True,
2793
+ init=True,
2794
+ inherited=False,
2795
+ )
2796
+ for name in Factory.__slots__
2797
+ ]
2798
+
2799
+ Factory = _add_hash(_add_eq(_add_repr(Factory, attrs=_f), attrs=_f), attrs=_f)
2800
+
2801
+
2802
+ class Converter:
2803
+ """
2804
+ Stores a converter callable.
2805
+
2806
+ Allows for the wrapped converter to take additional arguments. The
2807
+ arguments are passed in the order they are documented.
2808
+
2809
+ Args:
2810
+ converter (Callable): A callable that converts the passed value.
2811
+
2812
+ takes_self (bool):
2813
+ Pass the partially initialized instance that is being initialized
2814
+ as a positional argument. (default: `False`)
2815
+
2816
+ takes_field (bool):
2817
+ Pass the field definition (an :class:`Attribute`) into the
2818
+ converter as a positional argument. (default: `False`)
2819
+
2820
+ .. versionadded:: 24.1.0
2821
+ """
2822
+
2823
+ __slots__ = (
2824
+ "__call__",
2825
+ "_first_param_type",
2826
+ "_global_name",
2827
+ "converter",
2828
+ "takes_field",
2829
+ "takes_self",
2830
+ )
2831
+
2832
+ def __init__(self, converter, *, takes_self=False, takes_field=False):
2833
+ self.converter = converter
2834
+ self.takes_self = takes_self
2835
+ self.takes_field = takes_field
2836
+
2837
+ ex = _AnnotationExtractor(converter)
2838
+ self._first_param_type = ex.get_first_param_type()
2839
+
2840
+ if not (self.takes_self or self.takes_field):
2841
+ self.__call__ = lambda value, _, __: self.converter(value)
2842
+ elif self.takes_self and not self.takes_field:
2843
+ self.__call__ = lambda value, instance, __: self.converter(
2844
+ value, instance
2845
+ )
2846
+ elif not self.takes_self and self.takes_field:
2847
+ self.__call__ = lambda value, __, field: self.converter(
2848
+ value, field
2849
+ )
2850
+ else:
2851
+ self.__call__ = lambda value, instance, field: self.converter(
2852
+ value, instance, field
2853
+ )
2854
+
2855
+ rt = ex.get_return_type()
2856
+ if rt is not None:
2857
+ self.__call__.__annotations__["return"] = rt
2858
+
2859
+ @staticmethod
2860
+ def _get_global_name(attr_name: str) -> str:
2861
+ """
2862
+ Return the name that a converter for an attribute name *attr_name*
2863
+ would have.
2864
+ """
2865
+ return f"__attr_converter_{attr_name}"
2866
+
2867
+ def _fmt_converter_call(self, attr_name: str, value_var: str) -> str:
2868
+ """
2869
+ Return a string that calls the converter for an attribute name
2870
+ *attr_name* and the value in variable named *value_var* according to
2871
+ `self.takes_self` and `self.takes_field`.
2872
+ """
2873
+ if not (self.takes_self or self.takes_field):
2874
+ return f"{self._get_global_name(attr_name)}({value_var})"
2875
+
2876
+ if self.takes_self and self.takes_field:
2877
+ return f"{self._get_global_name(attr_name)}({value_var}, self, attr_dict['{attr_name}'])"
2878
+
2879
+ if self.takes_self:
2880
+ return f"{self._get_global_name(attr_name)}({value_var}, self)"
2881
+
2882
+ return f"{self._get_global_name(attr_name)}({value_var}, attr_dict['{attr_name}'])"
2883
+
2884
+ def __getstate__(self):
2885
+ """
2886
+ Return a dict containing only converter and takes_self -- the rest gets
2887
+ computed when loading.
2888
+ """
2889
+ return {
2890
+ "converter": self.converter,
2891
+ "takes_self": self.takes_self,
2892
+ "takes_field": self.takes_field,
2893
+ }
2894
+
2895
+ def __setstate__(self, state):
2896
+ """
2897
+ Load instance from state.
2898
+ """
2899
+ self.__init__(**state)
2900
+
2901
+
2902
+ _f = [
2903
+ Attribute(
2904
+ name=name,
2905
+ default=NOTHING,
2906
+ validator=None,
2907
+ repr=True,
2908
+ cmp=None,
2909
+ eq=True,
2910
+ order=False,
2911
+ hash=True,
2912
+ init=True,
2913
+ inherited=False,
2914
+ )
2915
+ for name in ("converter", "takes_self", "takes_field")
2916
+ ]
2917
+
2918
+ Converter = _add_hash(
2919
+ _add_eq(_add_repr(Converter, attrs=_f), attrs=_f), attrs=_f
2920
+ )
2921
+
2922
+
2923
+ def make_class(
2924
+ name, attrs, bases=(object,), class_body=None, **attributes_arguments
2925
+ ):
2926
+ r"""
2927
+ A quick way to create a new class called *name* with *attrs*.
2928
+
2929
+ .. note::
2930
+
2931
+ ``make_class()`` is a thin wrapper around `attr.s`, not `attrs.define`
2932
+ which means that it doesn't come with some of the improved defaults.
2933
+
2934
+ For example, if you want the same ``on_setattr`` behavior as in
2935
+ `attrs.define`, you have to pass the hooks yourself: ``make_class(...,
2936
+ on_setattr=setters.pipe(setters.convert, setters.validate)``
2937
+
2938
+ .. warning::
2939
+
2940
+ It is *your* duty to ensure that the class name and the attribute names
2941
+ are valid identifiers. ``make_class()`` will *not* validate them for
2942
+ you.
2943
+
2944
+ Args:
2945
+ name (str): The name for the new class.
2946
+
2947
+ attrs (list | dict):
2948
+ A list of names or a dictionary of mappings of names to `attr.ib`\
2949
+ s / `attrs.field`\ s.
2950
+
2951
+ The order is deduced from the order of the names or attributes
2952
+ inside *attrs*. Otherwise the order of the definition of the
2953
+ attributes is used.
2954
+
2955
+ bases (tuple[type, ...]): Classes that the new class will subclass.
2956
+
2957
+ class_body (dict):
2958
+ An optional dictionary of class attributes for the new class.
2959
+
2960
+ attributes_arguments: Passed unmodified to `attr.s`.
2961
+
2962
+ Returns:
2963
+ type: A new class with *attrs*.
2964
+
2965
+ .. versionadded:: 17.1.0 *bases*
2966
+ .. versionchanged:: 18.1.0 If *attrs* is ordered, the order is retained.
2967
+ .. versionchanged:: 23.2.0 *class_body*
2968
+ .. versionchanged:: 25.2.0 Class names can now be unicode.
2969
+ """
2970
+ # Class identifiers are converted into the normal form NFKC while parsing
2971
+ name = unicodedata.normalize("NFKC", name)
2972
+
2973
+ if isinstance(attrs, dict):
2974
+ cls_dict = attrs
2975
+ elif isinstance(attrs, (list, tuple)):
2976
+ cls_dict = {a: attrib() for a in attrs}
2977
+ else:
2978
+ msg = "attrs argument must be a dict or a list."
2979
+ raise TypeError(msg)
2980
+
2981
+ pre_init = cls_dict.pop("__attrs_pre_init__", None)
2982
+ post_init = cls_dict.pop("__attrs_post_init__", None)
2983
+ user_init = cls_dict.pop("__init__", None)
2984
+
2985
+ body = {}
2986
+ if class_body is not None:
2987
+ body.update(class_body)
2988
+ if pre_init is not None:
2989
+ body["__attrs_pre_init__"] = pre_init
2990
+ if post_init is not None:
2991
+ body["__attrs_post_init__"] = post_init
2992
+ if user_init is not None:
2993
+ body["__init__"] = user_init
2994
+
2995
+ type_ = types.new_class(name, bases, {}, lambda ns: ns.update(body))
2996
+
2997
+ # For pickling to work, the __module__ variable needs to be set to the
2998
+ # frame where the class is created. Bypass this step in environments where
2999
+ # sys._getframe is not defined (Jython for example) or sys._getframe is not
3000
+ # defined for arguments greater than 0 (IronPython).
3001
+ with contextlib.suppress(AttributeError, ValueError):
3002
+ type_.__module__ = sys._getframe(1).f_globals.get(
3003
+ "__name__", "__main__"
3004
+ )
3005
+
3006
+ # We do it here for proper warnings with meaningful stacklevel.
3007
+ cmp = attributes_arguments.pop("cmp", None)
3008
+ (
3009
+ attributes_arguments["eq"],
3010
+ attributes_arguments["order"],
3011
+ ) = _determine_attrs_eq_order(
3012
+ cmp,
3013
+ attributes_arguments.get("eq"),
3014
+ attributes_arguments.get("order"),
3015
+ True,
3016
+ )
3017
+
3018
+ cls = _attrs(these=cls_dict, **attributes_arguments)(type_)
3019
+ # Only add type annotations now or "_attrs()" will complain:
3020
+ cls.__annotations__ = {
3021
+ k: v.type for k, v in cls_dict.items() if v.type is not None
3022
+ }
3023
+ return cls
3024
+
3025
+
3026
+ # These are required by within this module so we define them here and merely
3027
+ # import into .validators / .converters.
3028
+
3029
+
3030
+ @attrs(slots=True, unsafe_hash=True)
3031
+ class _AndValidator:
3032
+ """
3033
+ Compose many validators to a single one.
3034
+ """
3035
+
3036
+ _validators = attrib()
3037
+
3038
+ def __call__(self, inst, attr, value):
3039
+ for v in self._validators:
3040
+ v(inst, attr, value)
3041
+
3042
+
3043
+ def and_(*validators):
3044
+ """
3045
+ A validator that composes multiple validators into one.
3046
+
3047
+ When called on a value, it runs all wrapped validators.
3048
+
3049
+ Args:
3050
+ validators (~collections.abc.Iterable[typing.Callable]):
3051
+ Arbitrary number of validators.
3052
+
3053
+ .. versionadded:: 17.1.0
3054
+ """
3055
+ vals = []
3056
+ for validator in validators:
3057
+ vals.extend(
3058
+ validator._validators
3059
+ if isinstance(validator, _AndValidator)
3060
+ else [validator]
3061
+ )
3062
+
3063
+ return _AndValidator(tuple(vals))
3064
+
3065
+
3066
+ def pipe(*converters):
3067
+ """
3068
+ A converter that composes multiple converters into one.
3069
+
3070
+ When called on a value, it runs all wrapped converters, returning the
3071
+ *last* value.
3072
+
3073
+ Type annotations will be inferred from the wrapped converters', if they
3074
+ have any.
3075
+
3076
+ converters (~collections.abc.Iterable[typing.Callable]):
3077
+ Arbitrary number of converters.
3078
+
3079
+ .. versionadded:: 20.1.0
3080
+ """
3081
+
3082
+ return_instance = any(isinstance(c, Converter) for c in converters)
3083
+
3084
+ if return_instance:
3085
+
3086
+ def pipe_converter(val, inst, field):
3087
+ for c in converters:
3088
+ val = (
3089
+ c(val, inst, field) if isinstance(c, Converter) else c(val)
3090
+ )
3091
+
3092
+ return val
3093
+
3094
+ else:
3095
+
3096
+ def pipe_converter(val):
3097
+ for c in converters:
3098
+ val = c(val)
3099
+
3100
+ return val
3101
+
3102
+ if not converters:
3103
+ # If the converter list is empty, pipe_converter is the identity.
3104
+ A = TypeVar("A")
3105
+ pipe_converter.__annotations__.update({"val": A, "return": A})
3106
+ else:
3107
+ # Get parameter type from first converter.
3108
+ t = _AnnotationExtractor(converters[0]).get_first_param_type()
3109
+ if t:
3110
+ pipe_converter.__annotations__["val"] = t
3111
+
3112
+ last = converters[-1]
3113
+ if not PY_3_11_PLUS and isinstance(last, Converter):
3114
+ last = last.__call__
3115
+
3116
+ # Get return type from last converter.
3117
+ rt = _AnnotationExtractor(last).get_return_type()
3118
+ if rt:
3119
+ pipe_converter.__annotations__["return"] = rt
3120
+
3121
+ if return_instance:
3122
+ return Converter(pipe_converter, takes_self=True, takes_field=True)
3123
+ return pipe_converter
venv/lib/python3.10/site-packages/attr/_next_gen.py ADDED
@@ -0,0 +1,623 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+
3
+ """
4
+ These are keyword-only APIs that call `attr.s` and `attr.ib` with different
5
+ default values.
6
+ """
7
+
8
+ from functools import partial
9
+
10
+ from . import setters
11
+ from ._funcs import asdict as _asdict
12
+ from ._funcs import astuple as _astuple
13
+ from ._make import (
14
+ _DEFAULT_ON_SETATTR,
15
+ NOTHING,
16
+ _frozen_setattrs,
17
+ attrib,
18
+ attrs,
19
+ )
20
+ from .exceptions import UnannotatedAttributeError
21
+
22
+
23
+ def define(
24
+ maybe_cls=None,
25
+ *,
26
+ these=None,
27
+ repr=None,
28
+ unsafe_hash=None,
29
+ hash=None,
30
+ init=None,
31
+ slots=True,
32
+ frozen=False,
33
+ weakref_slot=True,
34
+ str=False,
35
+ auto_attribs=None,
36
+ kw_only=False,
37
+ cache_hash=False,
38
+ auto_exc=True,
39
+ eq=None,
40
+ order=False,
41
+ auto_detect=True,
42
+ getstate_setstate=None,
43
+ on_setattr=None,
44
+ field_transformer=None,
45
+ match_args=True,
46
+ ):
47
+ r"""
48
+ A class decorator that adds :term:`dunder methods` according to
49
+ :term:`fields <field>` specified using :doc:`type annotations <types>`,
50
+ `field()` calls, or the *these* argument.
51
+
52
+ Since *attrs* patches or replaces an existing class, you cannot use
53
+ `object.__init_subclass__` with *attrs* classes, because it runs too early.
54
+ As a replacement, you can define ``__attrs_init_subclass__`` on your class.
55
+ It will be called by *attrs* classes that subclass it after they're
56
+ created. See also :ref:`init-subclass`.
57
+
58
+ Args:
59
+ slots (bool):
60
+ Create a :term:`slotted class <slotted classes>` that's more
61
+ memory-efficient. Slotted classes are generally superior to the
62
+ default dict classes, but have some gotchas you should know about,
63
+ so we encourage you to read the :term:`glossary entry <slotted
64
+ classes>`.
65
+
66
+ auto_detect (bool):
67
+ Instead of setting the *init*, *repr*, *eq*, and *hash* arguments
68
+ explicitly, assume they are set to True **unless any** of the
69
+ involved methods for one of the arguments is implemented in the
70
+ *current* class (meaning, it is *not* inherited from some base
71
+ class).
72
+
73
+ So, for example by implementing ``__eq__`` on a class yourself,
74
+ *attrs* will deduce ``eq=False`` and will create *neither*
75
+ ``__eq__`` *nor* ``__ne__`` (but Python classes come with a
76
+ sensible ``__ne__`` by default, so it *should* be enough to only
77
+ implement ``__eq__`` in most cases).
78
+
79
+ Passing True or False` to *init*, *repr*, *eq*, or *hash*
80
+ overrides whatever *auto_detect* would determine.
81
+
82
+ auto_exc (bool):
83
+ If the class subclasses `BaseException` (which implicitly includes
84
+ any subclass of any exception), the following happens to behave
85
+ like a well-behaved Python exception class:
86
+
87
+ - the values for *eq*, *order*, and *hash* are ignored and the
88
+ instances compare and hash by the instance's ids [#]_ ,
89
+ - all attributes that are either passed into ``__init__`` or have a
90
+ default value are additionally available as a tuple in the
91
+ ``args`` attribute,
92
+ - the value of *str* is ignored leaving ``__str__`` to base
93
+ classes.
94
+
95
+ .. [#]
96
+ Note that *attrs* will *not* remove existing implementations of
97
+ ``__hash__`` or the equality methods. It just won't add own
98
+ ones.
99
+
100
+ on_setattr (~typing.Callable | list[~typing.Callable] | None | ~typing.Literal[attrs.setters.NO_OP]):
101
+ A callable that is run whenever the user attempts to set an
102
+ attribute (either by assignment like ``i.x = 42`` or by using
103
+ `setattr` like ``setattr(i, "x", 42)``). It receives the same
104
+ arguments as validators: the instance, the attribute that is being
105
+ modified, and the new value.
106
+
107
+ If no exception is raised, the attribute is set to the return value
108
+ of the callable.
109
+
110
+ If a list of callables is passed, they're automatically wrapped in
111
+ an `attrs.setters.pipe`.
112
+
113
+ If left None, the default behavior is to run converters and
114
+ validators whenever an attribute is set.
115
+
116
+ init (bool):
117
+ Create a ``__init__`` method that initializes the *attrs*
118
+ attributes. Leading underscores are stripped for the argument name,
119
+ unless an alias is set on the attribute.
120
+
121
+ .. seealso::
122
+ `init` shows advanced ways to customize the generated
123
+ ``__init__`` method, including executing code before and after.
124
+
125
+ repr(bool):
126
+ Create a ``__repr__`` method with a human readable representation
127
+ of *attrs* attributes.
128
+
129
+ str (bool):
130
+ Create a ``__str__`` method that is identical to ``__repr__``. This
131
+ is usually not necessary except for `Exception`\ s.
132
+
133
+ eq (bool | None):
134
+ If True or None (default), add ``__eq__`` and ``__ne__`` methods
135
+ that check two instances for equality.
136
+
137
+ .. seealso::
138
+ `comparison` describes how to customize the comparison behavior
139
+ going as far comparing NumPy arrays.
140
+
141
+ order (bool | None):
142
+ If True, add ``__lt__``, ``__le__``, ``__gt__``, and ``__ge__``
143
+ methods that behave like *eq* above and allow instances to be
144
+ ordered.
145
+
146
+ They compare the instances as if they were tuples of their *attrs*
147
+ attributes if and only if the types of both classes are
148
+ *identical*.
149
+
150
+ If `None` mirror value of *eq*.
151
+
152
+ .. seealso:: `comparison`
153
+
154
+ unsafe_hash (bool | None):
155
+ If None (default), the ``__hash__`` method is generated according
156
+ how *eq* and *frozen* are set.
157
+
158
+ 1. If *both* are True, *attrs* will generate a ``__hash__`` for
159
+ you.
160
+ 2. If *eq* is True and *frozen* is False, ``__hash__`` will be set
161
+ to None, marking it unhashable (which it is).
162
+ 3. If *eq* is False, ``__hash__`` will be left untouched meaning
163
+ the ``__hash__`` method of the base class will be used. If the
164
+ base class is `object`, this means it will fall back to id-based
165
+ hashing.
166
+
167
+ Although not recommended, you can decide for yourself and force
168
+ *attrs* to create one (for example, if the class is immutable even
169
+ though you didn't freeze it programmatically) by passing True or
170
+ not. Both of these cases are rather special and should be used
171
+ carefully.
172
+
173
+ .. seealso::
174
+
175
+ - Our documentation on `hashing`,
176
+ - Python's documentation on `object.__hash__`,
177
+ - and the `GitHub issue that led to the default \ behavior
178
+ <https://github.com/python-attrs/attrs/issues/136>`_ for more
179
+ details.
180
+
181
+ hash (bool | None):
182
+ Deprecated alias for *unsafe_hash*. *unsafe_hash* takes precedence.
183
+
184
+ cache_hash (bool):
185
+ Ensure that the object's hash code is computed only once and stored
186
+ on the object. If this is set to True, hashing must be either
187
+ explicitly or implicitly enabled for this class. If the hash code
188
+ is cached, avoid any reassignments of fields involved in hash code
189
+ computation or mutations of the objects those fields point to after
190
+ object creation. If such changes occur, the behavior of the
191
+ object's hash code is undefined.
192
+
193
+ frozen (bool):
194
+ Make instances immutable after initialization. If someone attempts
195
+ to modify a frozen instance, `attrs.exceptions.FrozenInstanceError`
196
+ is raised.
197
+
198
+ .. note::
199
+
200
+ 1. This is achieved by installing a custom ``__setattr__``
201
+ method on your class, so you can't implement your own.
202
+
203
+ 2. True immutability is impossible in Python.
204
+
205
+ 3. This *does* have a minor a runtime performance `impact
206
+ <how-frozen>` when initializing new instances. In other
207
+ words: ``__init__`` is slightly slower with ``frozen=True``.
208
+
209
+ 4. If a class is frozen, you cannot modify ``self`` in
210
+ ``__attrs_post_init__`` or a self-written ``__init__``. You
211
+ can circumvent that limitation by using
212
+ ``object.__setattr__(self, "attribute_name", value)``.
213
+
214
+ 5. Subclasses of a frozen class are frozen too.
215
+
216
+ kw_only (bool):
217
+ Make all attributes keyword-only in the generated ``__init__`` (if
218
+ *init* is False, this parameter is ignored).
219
+
220
+ weakref_slot (bool):
221
+ Make instances weak-referenceable. This has no effect unless
222
+ *slots* is True.
223
+
224
+ field_transformer (~typing.Callable | None):
225
+ A function that is called with the original class object and all
226
+ fields right before *attrs* finalizes the class. You can use this,
227
+ for example, to automatically add converters or validators to
228
+ fields based on their types.
229
+
230
+ .. seealso:: `transform-fields`
231
+
232
+ match_args (bool):
233
+ If True (default), set ``__match_args__`` on the class to support
234
+ :pep:`634` (*Structural Pattern Matching*). It is a tuple of all
235
+ non-keyword-only ``__init__`` parameter names on Python 3.10 and
236
+ later. Ignored on older Python versions.
237
+
238
+ collect_by_mro (bool):
239
+ If True, *attrs* collects attributes from base classes correctly
240
+ according to the `method resolution order
241
+ <https://docs.python.org/3/howto/mro.html>`_. If False, *attrs*
242
+ will mimic the (wrong) behavior of `dataclasses` and :pep:`681`.
243
+
244
+ See also `issue #428
245
+ <https://github.com/python-attrs/attrs/issues/428>`_.
246
+
247
+ getstate_setstate (bool | None):
248
+ .. note::
249
+
250
+ This is usually only interesting for slotted classes and you
251
+ should probably just set *auto_detect* to True.
252
+
253
+ If True, ``__getstate__`` and ``__setstate__`` are generated and
254
+ attached to the class. This is necessary for slotted classes to be
255
+ pickleable. If left None, it's True by default for slotted classes
256
+ and False for dict classes.
257
+
258
+ If *auto_detect* is True, and *getstate_setstate* is left None, and
259
+ **either** ``__getstate__`` or ``__setstate__`` is detected
260
+ directly on the class (meaning: not inherited), it is set to False
261
+ (this is usually what you want).
262
+
263
+ auto_attribs (bool | None):
264
+ If True, look at type annotations to determine which attributes to
265
+ use, like `dataclasses`. If False, it will only look for explicit
266
+ :func:`field` class attributes, like classic *attrs*.
267
+
268
+ If left None, it will guess:
269
+
270
+ 1. If any attributes are annotated and no unannotated
271
+ `attrs.field`\ s are found, it assumes *auto_attribs=True*.
272
+ 2. Otherwise it assumes *auto_attribs=False* and tries to collect
273
+ `attrs.field`\ s.
274
+
275
+ If *attrs* decides to look at type annotations, **all** fields
276
+ **must** be annotated. If *attrs* encounters a field that is set to
277
+ a :func:`field` / `attr.ib` but lacks a type annotation, an
278
+ `attrs.exceptions.UnannotatedAttributeError` is raised. Use
279
+ ``field_name: typing.Any = field(...)`` if you don't want to set a
280
+ type.
281
+
282
+ .. warning::
283
+
284
+ For features that use the attribute name to create decorators
285
+ (for example, :ref:`validators <validators>`), you still *must*
286
+ assign :func:`field` / `attr.ib` to them. Otherwise Python will
287
+ either not find the name or try to use the default value to
288
+ call, for example, ``validator`` on it.
289
+
290
+ Attributes annotated as `typing.ClassVar`, and attributes that are
291
+ neither annotated nor set to an `field()` are **ignored**.
292
+
293
+ these (dict[str, object]):
294
+ A dictionary of name to the (private) return value of `field()`
295
+ mappings. This is useful to avoid the definition of your attributes
296
+ within the class body because you can't (for example, if you want
297
+ to add ``__repr__`` methods to Django models) or don't want to.
298
+
299
+ If *these* is not `None`, *attrs* will *not* search the class body
300
+ for attributes and will *not* remove any attributes from it.
301
+
302
+ The order is deduced from the order of the attributes inside
303
+ *these*.
304
+
305
+ Arguably, this is a rather obscure feature.
306
+
307
+ .. versionadded:: 20.1.0
308
+ .. versionchanged:: 21.3.0 Converters are also run ``on_setattr``.
309
+ .. versionadded:: 22.2.0
310
+ *unsafe_hash* as an alias for *hash* (for :pep:`681` compliance).
311
+ .. versionchanged:: 24.1.0
312
+ Instances are not compared as tuples of attributes anymore, but using a
313
+ big ``and`` condition. This is faster and has more correct behavior for
314
+ uncomparable values like `math.nan`.
315
+ .. versionadded:: 24.1.0
316
+ If a class has an *inherited* classmethod called
317
+ ``__attrs_init_subclass__``, it is executed after the class is created.
318
+ .. deprecated:: 24.1.0 *hash* is deprecated in favor of *unsafe_hash*.
319
+ .. versionadded:: 24.3.0
320
+ Unless already present, a ``__replace__`` method is automatically
321
+ created for `copy.replace` (Python 3.13+ only).
322
+
323
+ .. note::
324
+
325
+ The main differences to the classic `attr.s` are:
326
+
327
+ - Automatically detect whether or not *auto_attribs* should be `True`
328
+ (c.f. *auto_attribs* parameter).
329
+ - Converters and validators run when attributes are set by default --
330
+ if *frozen* is `False`.
331
+ - *slots=True*
332
+
333
+ Usually, this has only upsides and few visible effects in everyday
334
+ programming. But it *can* lead to some surprising behaviors, so
335
+ please make sure to read :term:`slotted classes`.
336
+
337
+ - *auto_exc=True*
338
+ - *auto_detect=True*
339
+ - *order=False*
340
+ - Some options that were only relevant on Python 2 or were kept around
341
+ for backwards-compatibility have been removed.
342
+
343
+ """
344
+
345
+ def do_it(cls, auto_attribs):
346
+ return attrs(
347
+ maybe_cls=cls,
348
+ these=these,
349
+ repr=repr,
350
+ hash=hash,
351
+ unsafe_hash=unsafe_hash,
352
+ init=init,
353
+ slots=slots,
354
+ frozen=frozen,
355
+ weakref_slot=weakref_slot,
356
+ str=str,
357
+ auto_attribs=auto_attribs,
358
+ kw_only=kw_only,
359
+ cache_hash=cache_hash,
360
+ auto_exc=auto_exc,
361
+ eq=eq,
362
+ order=order,
363
+ auto_detect=auto_detect,
364
+ collect_by_mro=True,
365
+ getstate_setstate=getstate_setstate,
366
+ on_setattr=on_setattr,
367
+ field_transformer=field_transformer,
368
+ match_args=match_args,
369
+ )
370
+
371
+ def wrap(cls):
372
+ """
373
+ Making this a wrapper ensures this code runs during class creation.
374
+
375
+ We also ensure that frozen-ness of classes is inherited.
376
+ """
377
+ nonlocal frozen, on_setattr
378
+
379
+ had_on_setattr = on_setattr not in (None, setters.NO_OP)
380
+
381
+ # By default, mutable classes convert & validate on setattr.
382
+ if frozen is False and on_setattr is None:
383
+ on_setattr = _DEFAULT_ON_SETATTR
384
+
385
+ # However, if we subclass a frozen class, we inherit the immutability
386
+ # and disable on_setattr.
387
+ for base_cls in cls.__bases__:
388
+ if base_cls.__setattr__ is _frozen_setattrs:
389
+ if had_on_setattr:
390
+ msg = "Frozen classes can't use on_setattr (frozen-ness was inherited)."
391
+ raise ValueError(msg)
392
+
393
+ on_setattr = setters.NO_OP
394
+ break
395
+
396
+ if auto_attribs is not None:
397
+ return do_it(cls, auto_attribs)
398
+
399
+ try:
400
+ return do_it(cls, True)
401
+ except UnannotatedAttributeError:
402
+ return do_it(cls, False)
403
+
404
+ # maybe_cls's type depends on the usage of the decorator. It's a class
405
+ # if it's used as `@attrs` but `None` if used as `@attrs()`.
406
+ if maybe_cls is None:
407
+ return wrap
408
+
409
+ return wrap(maybe_cls)
410
+
411
+
412
+ mutable = define
413
+ frozen = partial(define, frozen=True, on_setattr=None)
414
+
415
+
416
+ def field(
417
+ *,
418
+ default=NOTHING,
419
+ validator=None,
420
+ repr=True,
421
+ hash=None,
422
+ init=True,
423
+ metadata=None,
424
+ type=None,
425
+ converter=None,
426
+ factory=None,
427
+ kw_only=False,
428
+ eq=None,
429
+ order=None,
430
+ on_setattr=None,
431
+ alias=None,
432
+ ):
433
+ """
434
+ Create a new :term:`field` / :term:`attribute` on a class.
435
+
436
+ .. warning::
437
+
438
+ Does **nothing** unless the class is also decorated with
439
+ `attrs.define` (or similar)!
440
+
441
+ Args:
442
+ default:
443
+ A value that is used if an *attrs*-generated ``__init__`` is used
444
+ and no value is passed while instantiating or the attribute is
445
+ excluded using ``init=False``.
446
+
447
+ If the value is an instance of `attrs.Factory`, its callable will
448
+ be used to construct a new value (useful for mutable data types
449
+ like lists or dicts).
450
+
451
+ If a default is not set (or set manually to `attrs.NOTHING`), a
452
+ value *must* be supplied when instantiating; otherwise a
453
+ `TypeError` will be raised.
454
+
455
+ .. seealso:: `defaults`
456
+
457
+ factory (~typing.Callable):
458
+ Syntactic sugar for ``default=attr.Factory(factory)``.
459
+
460
+ validator (~typing.Callable | list[~typing.Callable]):
461
+ Callable that is called by *attrs*-generated ``__init__`` methods
462
+ after the instance has been initialized. They receive the
463
+ initialized instance, the :func:`~attrs.Attribute`, and the passed
464
+ value.
465
+
466
+ The return value is *not* inspected so the validator has to throw
467
+ an exception itself.
468
+
469
+ If a `list` is passed, its items are treated as validators and must
470
+ all pass.
471
+
472
+ Validators can be globally disabled and re-enabled using
473
+ `attrs.validators.get_disabled` / `attrs.validators.set_disabled`.
474
+
475
+ The validator can also be set using decorator notation as shown
476
+ below.
477
+
478
+ .. seealso:: :ref:`validators`
479
+
480
+ repr (bool | ~typing.Callable):
481
+ Include this attribute in the generated ``__repr__`` method. If
482
+ True, include the attribute; if False, omit it. By default, the
483
+ built-in ``repr()`` function is used. To override how the attribute
484
+ value is formatted, pass a ``callable`` that takes a single value
485
+ and returns a string. Note that the resulting string is used as-is,
486
+ which means it will be used directly *instead* of calling
487
+ ``repr()`` (the default).
488
+
489
+ eq (bool | ~typing.Callable):
490
+ If True (default), include this attribute in the generated
491
+ ``__eq__`` and ``__ne__`` methods that check two instances for
492
+ equality. To override how the attribute value is compared, pass a
493
+ callable that takes a single value and returns the value to be
494
+ compared.
495
+
496
+ .. seealso:: `comparison`
497
+
498
+ order (bool | ~typing.Callable):
499
+ If True (default), include this attributes in the generated
500
+ ``__lt__``, ``__le__``, ``__gt__`` and ``__ge__`` methods. To
501
+ override how the attribute value is ordered, pass a callable that
502
+ takes a single value and returns the value to be ordered.
503
+
504
+ .. seealso:: `comparison`
505
+
506
+ hash (bool | None):
507
+ Include this attribute in the generated ``__hash__`` method. If
508
+ None (default), mirror *eq*'s value. This is the correct behavior
509
+ according the Python spec. Setting this value to anything else
510
+ than None is *discouraged*.
511
+
512
+ .. seealso:: `hashing`
513
+
514
+ init (bool):
515
+ Include this attribute in the generated ``__init__`` method.
516
+
517
+ It is possible to set this to False and set a default value. In
518
+ that case this attributed is unconditionally initialized with the
519
+ specified default value or factory.
520
+
521
+ .. seealso:: `init`
522
+
523
+ converter (typing.Callable | Converter):
524
+ A callable that is called by *attrs*-generated ``__init__`` methods
525
+ to convert attribute's value to the desired format.
526
+
527
+ If a vanilla callable is passed, it is given the passed-in value as
528
+ the only positional argument. It is possible to receive additional
529
+ arguments by wrapping the callable in a `Converter`.
530
+
531
+ Either way, the returned value will be used as the new value of the
532
+ attribute. The value is converted before being passed to the
533
+ validator, if any.
534
+
535
+ .. seealso:: :ref:`converters`
536
+
537
+ metadata (dict | None):
538
+ An arbitrary mapping, to be used by third-party code.
539
+
540
+ .. seealso:: `extending-metadata`.
541
+
542
+ type (type):
543
+ The type of the attribute. Nowadays, the preferred method to
544
+ specify the type is using a variable annotation (see :pep:`526`).
545
+ This argument is provided for backwards-compatibility and for usage
546
+ with `make_class`. Regardless of the approach used, the type will
547
+ be stored on ``Attribute.type``.
548
+
549
+ Please note that *attrs* doesn't do anything with this metadata by
550
+ itself. You can use it as part of your own code or for `static type
551
+ checking <types>`.
552
+
553
+ kw_only (bool):
554
+ Make this attribute keyword-only in the generated ``__init__`` (if
555
+ ``init`` is False, this parameter is ignored).
556
+
557
+ on_setattr (~typing.Callable | list[~typing.Callable] | None | ~typing.Literal[attrs.setters.NO_OP]):
558
+ Allows to overwrite the *on_setattr* setting from `attr.s`. If left
559
+ None, the *on_setattr* value from `attr.s` is used. Set to
560
+ `attrs.setters.NO_OP` to run **no** `setattr` hooks for this
561
+ attribute -- regardless of the setting in `define()`.
562
+
563
+ alias (str | None):
564
+ Override this attribute's parameter name in the generated
565
+ ``__init__`` method. If left None, default to ``name`` stripped
566
+ of leading underscores. See `private-attributes`.
567
+
568
+ .. versionadded:: 20.1.0
569
+ .. versionchanged:: 21.1.0
570
+ *eq*, *order*, and *cmp* also accept a custom callable
571
+ .. versionadded:: 22.2.0 *alias*
572
+ .. versionadded:: 23.1.0
573
+ The *type* parameter has been re-added; mostly for `attrs.make_class`.
574
+ Please note that type checkers ignore this metadata.
575
+
576
+ .. seealso::
577
+
578
+ `attr.ib`
579
+ """
580
+ return attrib(
581
+ default=default,
582
+ validator=validator,
583
+ repr=repr,
584
+ hash=hash,
585
+ init=init,
586
+ metadata=metadata,
587
+ type=type,
588
+ converter=converter,
589
+ factory=factory,
590
+ kw_only=kw_only,
591
+ eq=eq,
592
+ order=order,
593
+ on_setattr=on_setattr,
594
+ alias=alias,
595
+ )
596
+
597
+
598
+ def asdict(inst, *, recurse=True, filter=None, value_serializer=None):
599
+ """
600
+ Same as `attr.asdict`, except that collections types are always retained
601
+ and dict is always used as *dict_factory*.
602
+
603
+ .. versionadded:: 21.3.0
604
+ """
605
+ return _asdict(
606
+ inst=inst,
607
+ recurse=recurse,
608
+ filter=filter,
609
+ value_serializer=value_serializer,
610
+ retain_collection_types=True,
611
+ )
612
+
613
+
614
+ def astuple(inst, *, recurse=True, filter=None):
615
+ """
616
+ Same as `attr.astuple`, except that collections types are always retained
617
+ and `tuple` is always used as the *tuple_factory*.
618
+
619
+ .. versionadded:: 21.3.0
620
+ """
621
+ return _astuple(
622
+ inst=inst, recurse=recurse, filter=filter, retain_collection_types=True
623
+ )
venv/lib/python3.10/site-packages/attr/_typing_compat.pyi ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, ClassVar, Protocol
2
+
3
+ # MYPY is a special constant in mypy which works the same way as `TYPE_CHECKING`.
4
+ MYPY = False
5
+
6
+ if MYPY:
7
+ # A protocol to be able to statically accept an attrs class.
8
+ class AttrsInstance_(Protocol):
9
+ __attrs_attrs__: ClassVar[Any]
10
+
11
+ else:
12
+ # For type checkers without plug-in support use an empty protocol that
13
+ # will (hopefully) be combined into a union.
14
+ class AttrsInstance_(Protocol):
15
+ pass
venv/lib/python3.10/site-packages/attr/_version_info.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+
3
+
4
+ from functools import total_ordering
5
+
6
+ from ._funcs import astuple
7
+ from ._make import attrib, attrs
8
+
9
+
10
+ @total_ordering
11
+ @attrs(eq=False, order=False, slots=True, frozen=True)
12
+ class VersionInfo:
13
+ """
14
+ A version object that can be compared to tuple of length 1--4:
15
+
16
+ >>> attr.VersionInfo(19, 1, 0, "final") <= (19, 2)
17
+ True
18
+ >>> attr.VersionInfo(19, 1, 0, "final") < (19, 1, 1)
19
+ True
20
+ >>> vi = attr.VersionInfo(19, 2, 0, "final")
21
+ >>> vi < (19, 1, 1)
22
+ False
23
+ >>> vi < (19,)
24
+ False
25
+ >>> vi == (19, 2,)
26
+ True
27
+ >>> vi == (19, 2, 1)
28
+ False
29
+
30
+ .. versionadded:: 19.2
31
+ """
32
+
33
+ year = attrib(type=int)
34
+ minor = attrib(type=int)
35
+ micro = attrib(type=int)
36
+ releaselevel = attrib(type=str)
37
+
38
+ @classmethod
39
+ def _from_version_string(cls, s):
40
+ """
41
+ Parse *s* and return a _VersionInfo.
42
+ """
43
+ v = s.split(".")
44
+ if len(v) == 3:
45
+ v.append("final")
46
+
47
+ return cls(
48
+ year=int(v[0]), minor=int(v[1]), micro=int(v[2]), releaselevel=v[3]
49
+ )
50
+
51
+ def _ensure_tuple(self, other):
52
+ """
53
+ Ensure *other* is a tuple of a valid length.
54
+
55
+ Returns a possibly transformed *other* and ourselves as a tuple of
56
+ the same length as *other*.
57
+ """
58
+
59
+ if self.__class__ is other.__class__:
60
+ other = astuple(other)
61
+
62
+ if not isinstance(other, tuple):
63
+ raise NotImplementedError
64
+
65
+ if not (1 <= len(other) <= 4):
66
+ raise NotImplementedError
67
+
68
+ return astuple(self)[: len(other)], other
69
+
70
+ def __eq__(self, other):
71
+ try:
72
+ us, them = self._ensure_tuple(other)
73
+ except NotImplementedError:
74
+ return NotImplemented
75
+
76
+ return us == them
77
+
78
+ def __lt__(self, other):
79
+ try:
80
+ us, them = self._ensure_tuple(other)
81
+ except NotImplementedError:
82
+ return NotImplemented
83
+
84
+ # Since alphabetically "dev0" < "final" < "post1" < "post2", we don't
85
+ # have to do anything special with releaselevel for now.
86
+ return us < them
venv/lib/python3.10/site-packages/attr/_version_info.pyi ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ class VersionInfo:
2
+ @property
3
+ def year(self) -> int: ...
4
+ @property
5
+ def minor(self) -> int: ...
6
+ @property
7
+ def micro(self) -> int: ...
8
+ @property
9
+ def releaselevel(self) -> str: ...
venv/lib/python3.10/site-packages/attr/converters.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+
3
+ """
4
+ Commonly useful converters.
5
+ """
6
+
7
+ import typing
8
+
9
+ from ._compat import _AnnotationExtractor
10
+ from ._make import NOTHING, Converter, Factory, pipe
11
+
12
+
13
+ __all__ = [
14
+ "default_if_none",
15
+ "optional",
16
+ "pipe",
17
+ "to_bool",
18
+ ]
19
+
20
+
21
+ def optional(converter):
22
+ """
23
+ A converter that allows an attribute to be optional. An optional attribute
24
+ is one which can be set to `None`.
25
+
26
+ Type annotations will be inferred from the wrapped converter's, if it has
27
+ any.
28
+
29
+ Args:
30
+ converter (typing.Callable):
31
+ the converter that is used for non-`None` values.
32
+
33
+ .. versionadded:: 17.1.0
34
+ """
35
+
36
+ if isinstance(converter, Converter):
37
+
38
+ def optional_converter(val, inst, field):
39
+ if val is None:
40
+ return None
41
+ return converter(val, inst, field)
42
+
43
+ else:
44
+
45
+ def optional_converter(val):
46
+ if val is None:
47
+ return None
48
+ return converter(val)
49
+
50
+ xtr = _AnnotationExtractor(converter)
51
+
52
+ t = xtr.get_first_param_type()
53
+ if t:
54
+ optional_converter.__annotations__["val"] = typing.Optional[t]
55
+
56
+ rt = xtr.get_return_type()
57
+ if rt:
58
+ optional_converter.__annotations__["return"] = typing.Optional[rt]
59
+
60
+ if isinstance(converter, Converter):
61
+ return Converter(optional_converter, takes_self=True, takes_field=True)
62
+
63
+ return optional_converter
64
+
65
+
66
+ def default_if_none(default=NOTHING, factory=None):
67
+ """
68
+ A converter that allows to replace `None` values by *default* or the result
69
+ of *factory*.
70
+
71
+ Args:
72
+ default:
73
+ Value to be used if `None` is passed. Passing an instance of
74
+ `attrs.Factory` is supported, however the ``takes_self`` option is
75
+ *not*.
76
+
77
+ factory (typing.Callable):
78
+ A callable that takes no parameters whose result is used if `None`
79
+ is passed.
80
+
81
+ Raises:
82
+ TypeError: If **neither** *default* or *factory* is passed.
83
+
84
+ TypeError: If **both** *default* and *factory* are passed.
85
+
86
+ ValueError:
87
+ If an instance of `attrs.Factory` is passed with
88
+ ``takes_self=True``.
89
+
90
+ .. versionadded:: 18.2.0
91
+ """
92
+ if default is NOTHING and factory is None:
93
+ msg = "Must pass either `default` or `factory`."
94
+ raise TypeError(msg)
95
+
96
+ if default is not NOTHING and factory is not None:
97
+ msg = "Must pass either `default` or `factory` but not both."
98
+ raise TypeError(msg)
99
+
100
+ if factory is not None:
101
+ default = Factory(factory)
102
+
103
+ if isinstance(default, Factory):
104
+ if default.takes_self:
105
+ msg = "`takes_self` is not supported by default_if_none."
106
+ raise ValueError(msg)
107
+
108
+ def default_if_none_converter(val):
109
+ if val is not None:
110
+ return val
111
+
112
+ return default.factory()
113
+
114
+ else:
115
+
116
+ def default_if_none_converter(val):
117
+ if val is not None:
118
+ return val
119
+
120
+ return default
121
+
122
+ return default_if_none_converter
123
+
124
+
125
+ def to_bool(val):
126
+ """
127
+ Convert "boolean" strings (for example, from environment variables) to real
128
+ booleans.
129
+
130
+ Values mapping to `True`:
131
+
132
+ - ``True``
133
+ - ``"true"`` / ``"t"``
134
+ - ``"yes"`` / ``"y"``
135
+ - ``"on"``
136
+ - ``"1"``
137
+ - ``1``
138
+
139
+ Values mapping to `False`:
140
+
141
+ - ``False``
142
+ - ``"false"`` / ``"f"``
143
+ - ``"no"`` / ``"n"``
144
+ - ``"off"``
145
+ - ``"0"``
146
+ - ``0``
147
+
148
+ Raises:
149
+ ValueError: For any other value.
150
+
151
+ .. versionadded:: 21.3.0
152
+ """
153
+ if isinstance(val, str):
154
+ val = val.lower()
155
+
156
+ if val in (True, "true", "t", "yes", "y", "on", "1", 1):
157
+ return True
158
+ if val in (False, "false", "f", "no", "n", "off", "0", 0):
159
+ return False
160
+
161
+ msg = f"Cannot convert value to bool: {val!r}"
162
+ raise ValueError(msg)
venv/lib/python3.10/site-packages/attr/converters.pyi ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Callable, Any, overload
2
+
3
+ from attrs import _ConverterType, _CallableConverterType
4
+
5
+ @overload
6
+ def pipe(*validators: _CallableConverterType) -> _CallableConverterType: ...
7
+ @overload
8
+ def pipe(*validators: _ConverterType) -> _ConverterType: ...
9
+ @overload
10
+ def optional(converter: _CallableConverterType) -> _CallableConverterType: ...
11
+ @overload
12
+ def optional(converter: _ConverterType) -> _ConverterType: ...
13
+ @overload
14
+ def default_if_none(default: Any) -> _CallableConverterType: ...
15
+ @overload
16
+ def default_if_none(
17
+ *, factory: Callable[[], Any]
18
+ ) -> _CallableConverterType: ...
19
+ def to_bool(val: str | int | bool) -> bool: ...
venv/lib/python3.10/site-packages/attr/exceptions.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import ClassVar
6
+
7
+
8
+ class FrozenError(AttributeError):
9
+ """
10
+ A frozen/immutable instance or attribute have been attempted to be
11
+ modified.
12
+
13
+ It mirrors the behavior of ``namedtuples`` by using the same error message
14
+ and subclassing `AttributeError`.
15
+
16
+ .. versionadded:: 20.1.0
17
+ """
18
+
19
+ msg = "can't set attribute"
20
+ args: ClassVar[tuple[str]] = [msg]
21
+
22
+
23
+ class FrozenInstanceError(FrozenError):
24
+ """
25
+ A frozen instance has been attempted to be modified.
26
+
27
+ .. versionadded:: 16.1.0
28
+ """
29
+
30
+
31
+ class FrozenAttributeError(FrozenError):
32
+ """
33
+ A frozen attribute has been attempted to be modified.
34
+
35
+ .. versionadded:: 20.1.0
36
+ """
37
+
38
+
39
+ class AttrsAttributeNotFoundError(ValueError):
40
+ """
41
+ An *attrs* function couldn't find an attribute that the user asked for.
42
+
43
+ .. versionadded:: 16.2.0
44
+ """
45
+
46
+
47
+ class NotAnAttrsClassError(ValueError):
48
+ """
49
+ A non-*attrs* class has been passed into an *attrs* function.
50
+
51
+ .. versionadded:: 16.2.0
52
+ """
53
+
54
+
55
+ class DefaultAlreadySetError(RuntimeError):
56
+ """
57
+ A default has been set when defining the field and is attempted to be reset
58
+ using the decorator.
59
+
60
+ .. versionadded:: 17.1.0
61
+ """
62
+
63
+
64
+ class UnannotatedAttributeError(RuntimeError):
65
+ """
66
+ A class with ``auto_attribs=True`` has a field without a type annotation.
67
+
68
+ .. versionadded:: 17.3.0
69
+ """
70
+
71
+
72
+ class PythonTooOldError(RuntimeError):
73
+ """
74
+ It was attempted to use an *attrs* feature that requires a newer Python
75
+ version.
76
+
77
+ .. versionadded:: 18.2.0
78
+ """
79
+
80
+
81
+ class NotCallableError(TypeError):
82
+ """
83
+ A field requiring a callable has been set with a value that is not
84
+ callable.
85
+
86
+ .. versionadded:: 19.2.0
87
+ """
88
+
89
+ def __init__(self, msg, value):
90
+ super(TypeError, self).__init__(msg, value)
91
+ self.msg = msg
92
+ self.value = value
93
+
94
+ def __str__(self):
95
+ return str(self.msg)
venv/lib/python3.10/site-packages/attr/exceptions.pyi ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any
2
+
3
+ class FrozenError(AttributeError):
4
+ msg: str = ...
5
+
6
+ class FrozenInstanceError(FrozenError): ...
7
+ class FrozenAttributeError(FrozenError): ...
8
+ class AttrsAttributeNotFoundError(ValueError): ...
9
+ class NotAnAttrsClassError(ValueError): ...
10
+ class DefaultAlreadySetError(RuntimeError): ...
11
+ class UnannotatedAttributeError(RuntimeError): ...
12
+ class PythonTooOldError(RuntimeError): ...
13
+
14
+ class NotCallableError(TypeError):
15
+ msg: str = ...
16
+ value: Any = ...
17
+ def __init__(self, msg: str, value: Any) -> None: ...
venv/lib/python3.10/site-packages/attr/filters.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+
3
+ """
4
+ Commonly useful filters for `attrs.asdict` and `attrs.astuple`.
5
+ """
6
+
7
+ from ._make import Attribute
8
+
9
+
10
+ def _split_what(what):
11
+ """
12
+ Returns a tuple of `frozenset`s of classes and attributes.
13
+ """
14
+ return (
15
+ frozenset(cls for cls in what if isinstance(cls, type)),
16
+ frozenset(cls for cls in what if isinstance(cls, str)),
17
+ frozenset(cls for cls in what if isinstance(cls, Attribute)),
18
+ )
19
+
20
+
21
+ def include(*what):
22
+ """
23
+ Create a filter that only allows *what*.
24
+
25
+ Args:
26
+ what (list[type, str, attrs.Attribute]):
27
+ What to include. Can be a type, a name, or an attribute.
28
+
29
+ Returns:
30
+ Callable:
31
+ A callable that can be passed to `attrs.asdict`'s and
32
+ `attrs.astuple`'s *filter* argument.
33
+
34
+ .. versionchanged:: 23.1.0 Accept strings with field names.
35
+ """
36
+ cls, names, attrs = _split_what(what)
37
+
38
+ def include_(attribute, value):
39
+ return (
40
+ value.__class__ in cls
41
+ or attribute.name in names
42
+ or attribute in attrs
43
+ )
44
+
45
+ return include_
46
+
47
+
48
+ def exclude(*what):
49
+ """
50
+ Create a filter that does **not** allow *what*.
51
+
52
+ Args:
53
+ what (list[type, str, attrs.Attribute]):
54
+ What to exclude. Can be a type, a name, or an attribute.
55
+
56
+ Returns:
57
+ Callable:
58
+ A callable that can be passed to `attrs.asdict`'s and
59
+ `attrs.astuple`'s *filter* argument.
60
+
61
+ .. versionchanged:: 23.3.0 Accept field name string as input argument
62
+ """
63
+ cls, names, attrs = _split_what(what)
64
+
65
+ def exclude_(attribute, value):
66
+ return not (
67
+ value.__class__ in cls
68
+ or attribute.name in names
69
+ or attribute in attrs
70
+ )
71
+
72
+ return exclude_
venv/lib/python3.10/site-packages/attr/filters.pyi ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ from typing import Any
2
+
3
+ from . import Attribute, _FilterType
4
+
5
+ def include(*what: type | str | Attribute[Any]) -> _FilterType[Any]: ...
6
+ def exclude(*what: type | str | Attribute[Any]) -> _FilterType[Any]: ...
venv/lib/python3.10/site-packages/attr/py.typed ADDED
File without changes
venv/lib/python3.10/site-packages/attr/setters.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+
3
+ """
4
+ Commonly used hooks for on_setattr.
5
+ """
6
+
7
+ from . import _config
8
+ from .exceptions import FrozenAttributeError
9
+
10
+
11
+ def pipe(*setters):
12
+ """
13
+ Run all *setters* and return the return value of the last one.
14
+
15
+ .. versionadded:: 20.1.0
16
+ """
17
+
18
+ def wrapped_pipe(instance, attrib, new_value):
19
+ rv = new_value
20
+
21
+ for setter in setters:
22
+ rv = setter(instance, attrib, rv)
23
+
24
+ return rv
25
+
26
+ return wrapped_pipe
27
+
28
+
29
+ def frozen(_, __, ___):
30
+ """
31
+ Prevent an attribute to be modified.
32
+
33
+ .. versionadded:: 20.1.0
34
+ """
35
+ raise FrozenAttributeError
36
+
37
+
38
+ def validate(instance, attrib, new_value):
39
+ """
40
+ Run *attrib*'s validator on *new_value* if it has one.
41
+
42
+ .. versionadded:: 20.1.0
43
+ """
44
+ if _config._run_validators is False:
45
+ return new_value
46
+
47
+ v = attrib.validator
48
+ if not v:
49
+ return new_value
50
+
51
+ v(instance, attrib, new_value)
52
+
53
+ return new_value
54
+
55
+
56
+ def convert(instance, attrib, new_value):
57
+ """
58
+ Run *attrib*'s converter -- if it has one -- on *new_value* and return the
59
+ result.
60
+
61
+ .. versionadded:: 20.1.0
62
+ """
63
+ c = attrib.converter
64
+ if c:
65
+ # This can be removed once we drop 3.8 and use attrs.Converter instead.
66
+ from ._make import Converter
67
+
68
+ if not isinstance(c, Converter):
69
+ return c(new_value)
70
+
71
+ return c(new_value, instance, attrib)
72
+
73
+ return new_value
74
+
75
+
76
+ # Sentinel for disabling class-wide *on_setattr* hooks for certain attributes.
77
+ # Sphinx's autodata stopped working, so the docstring is inlined in the API
78
+ # docs.
79
+ NO_OP = object()
venv/lib/python3.10/site-packages/attr/setters.pyi ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, NewType, NoReturn, TypeVar
2
+
3
+ from . import Attribute
4
+ from attrs import _OnSetAttrType
5
+
6
+ _T = TypeVar("_T")
7
+
8
+ def frozen(
9
+ instance: Any, attribute: Attribute[Any], new_value: Any
10
+ ) -> NoReturn: ...
11
+ def pipe(*setters: _OnSetAttrType) -> _OnSetAttrType: ...
12
+ def validate(instance: Any, attribute: Attribute[_T], new_value: _T) -> _T: ...
13
+
14
+ # convert is allowed to return Any, because they can be chained using pipe.
15
+ def convert(
16
+ instance: Any, attribute: Attribute[Any], new_value: Any
17
+ ) -> Any: ...
18
+
19
+ _NoOpType = NewType("_NoOpType", object)
20
+ NO_OP: _NoOpType
venv/lib/python3.10/site-packages/attr/validators.py ADDED
@@ -0,0 +1,710 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: MIT
2
+
3
+ """
4
+ Commonly useful validators.
5
+ """
6
+
7
+ import operator
8
+ import re
9
+
10
+ from contextlib import contextmanager
11
+ from re import Pattern
12
+
13
+ from ._config import get_run_validators, set_run_validators
14
+ from ._make import _AndValidator, and_, attrib, attrs
15
+ from .converters import default_if_none
16
+ from .exceptions import NotCallableError
17
+
18
+
19
+ __all__ = [
20
+ "and_",
21
+ "deep_iterable",
22
+ "deep_mapping",
23
+ "disabled",
24
+ "ge",
25
+ "get_disabled",
26
+ "gt",
27
+ "in_",
28
+ "instance_of",
29
+ "is_callable",
30
+ "le",
31
+ "lt",
32
+ "matches_re",
33
+ "max_len",
34
+ "min_len",
35
+ "not_",
36
+ "optional",
37
+ "or_",
38
+ "set_disabled",
39
+ ]
40
+
41
+
42
+ def set_disabled(disabled):
43
+ """
44
+ Globally disable or enable running validators.
45
+
46
+ By default, they are run.
47
+
48
+ Args:
49
+ disabled (bool): If `True`, disable running all validators.
50
+
51
+ .. warning::
52
+
53
+ This function is not thread-safe!
54
+
55
+ .. versionadded:: 21.3.0
56
+ """
57
+ set_run_validators(not disabled)
58
+
59
+
60
+ def get_disabled():
61
+ """
62
+ Return a bool indicating whether validators are currently disabled or not.
63
+
64
+ Returns:
65
+ bool:`True` if validators are currently disabled.
66
+
67
+ .. versionadded:: 21.3.0
68
+ """
69
+ return not get_run_validators()
70
+
71
+
72
+ @contextmanager
73
+ def disabled():
74
+ """
75
+ Context manager that disables running validators within its context.
76
+
77
+ .. warning::
78
+
79
+ This context manager is not thread-safe!
80
+
81
+ .. versionadded:: 21.3.0
82
+ """
83
+ set_run_validators(False)
84
+ try:
85
+ yield
86
+ finally:
87
+ set_run_validators(True)
88
+
89
+
90
+ @attrs(repr=False, slots=True, unsafe_hash=True)
91
+ class _InstanceOfValidator:
92
+ type = attrib()
93
+
94
+ def __call__(self, inst, attr, value):
95
+ """
96
+ We use a callable class to be able to change the ``__repr__``.
97
+ """
98
+ if not isinstance(value, self.type):
99
+ msg = f"'{attr.name}' must be {self.type!r} (got {value!r} that is a {value.__class__!r})."
100
+ raise TypeError(
101
+ msg,
102
+ attr,
103
+ self.type,
104
+ value,
105
+ )
106
+
107
+ def __repr__(self):
108
+ return f"<instance_of validator for type {self.type!r}>"
109
+
110
+
111
+ def instance_of(type):
112
+ """
113
+ A validator that raises a `TypeError` if the initializer is called with a
114
+ wrong type for this particular attribute (checks are performed using
115
+ `isinstance` therefore it's also valid to pass a tuple of types).
116
+
117
+ Args:
118
+ type (type | tuple[type]): The type to check for.
119
+
120
+ Raises:
121
+ TypeError:
122
+ With a human readable error message, the attribute (of type
123
+ `attrs.Attribute`), the expected type, and the value it got.
124
+ """
125
+ return _InstanceOfValidator(type)
126
+
127
+
128
+ @attrs(repr=False, frozen=True, slots=True)
129
+ class _MatchesReValidator:
130
+ pattern = attrib()
131
+ match_func = attrib()
132
+
133
+ def __call__(self, inst, attr, value):
134
+ """
135
+ We use a callable class to be able to change the ``__repr__``.
136
+ """
137
+ if not self.match_func(value):
138
+ msg = f"'{attr.name}' must match regex {self.pattern.pattern!r} ({value!r} doesn't)"
139
+ raise ValueError(
140
+ msg,
141
+ attr,
142
+ self.pattern,
143
+ value,
144
+ )
145
+
146
+ def __repr__(self):
147
+ return f"<matches_re validator for pattern {self.pattern!r}>"
148
+
149
+
150
+ def matches_re(regex, flags=0, func=None):
151
+ r"""
152
+ A validator that raises `ValueError` if the initializer is called with a
153
+ string that doesn't match *regex*.
154
+
155
+ Args:
156
+ regex (str, re.Pattern):
157
+ A regex string or precompiled pattern to match against
158
+
159
+ flags (int):
160
+ Flags that will be passed to the underlying re function (default 0)
161
+
162
+ func (typing.Callable):
163
+ Which underlying `re` function to call. Valid options are
164
+ `re.fullmatch`, `re.search`, and `re.match`; the default `None`
165
+ means `re.fullmatch`. For performance reasons, the pattern is
166
+ always precompiled using `re.compile`.
167
+
168
+ .. versionadded:: 19.2.0
169
+ .. versionchanged:: 21.3.0 *regex* can be a pre-compiled pattern.
170
+ """
171
+ valid_funcs = (re.fullmatch, None, re.search, re.match)
172
+ if func not in valid_funcs:
173
+ msg = "'func' must be one of {}.".format(
174
+ ", ".join(
175
+ sorted((e and e.__name__) or "None" for e in set(valid_funcs))
176
+ )
177
+ )
178
+ raise ValueError(msg)
179
+
180
+ if isinstance(regex, Pattern):
181
+ if flags:
182
+ msg = "'flags' can only be used with a string pattern; pass flags to re.compile() instead"
183
+ raise TypeError(msg)
184
+ pattern = regex
185
+ else:
186
+ pattern = re.compile(regex, flags)
187
+
188
+ if func is re.match:
189
+ match_func = pattern.match
190
+ elif func is re.search:
191
+ match_func = pattern.search
192
+ else:
193
+ match_func = pattern.fullmatch
194
+
195
+ return _MatchesReValidator(pattern, match_func)
196
+
197
+
198
+ @attrs(repr=False, slots=True, unsafe_hash=True)
199
+ class _OptionalValidator:
200
+ validator = attrib()
201
+
202
+ def __call__(self, inst, attr, value):
203
+ if value is None:
204
+ return
205
+
206
+ self.validator(inst, attr, value)
207
+
208
+ def __repr__(self):
209
+ return f"<optional validator for {self.validator!r} or None>"
210
+
211
+
212
+ def optional(validator):
213
+ """
214
+ A validator that makes an attribute optional. An optional attribute is one
215
+ which can be set to `None` in addition to satisfying the requirements of
216
+ the sub-validator.
217
+
218
+ Args:
219
+ validator
220
+ (typing.Callable | tuple[typing.Callable] | list[typing.Callable]):
221
+ A validator (or validators) that is used for non-`None` values.
222
+
223
+ .. versionadded:: 15.1.0
224
+ .. versionchanged:: 17.1.0 *validator* can be a list of validators.
225
+ .. versionchanged:: 23.1.0 *validator* can also be a tuple of validators.
226
+ """
227
+ if isinstance(validator, (list, tuple)):
228
+ return _OptionalValidator(_AndValidator(validator))
229
+
230
+ return _OptionalValidator(validator)
231
+
232
+
233
+ @attrs(repr=False, slots=True, unsafe_hash=True)
234
+ class _InValidator:
235
+ options = attrib()
236
+ _original_options = attrib(hash=False)
237
+
238
+ def __call__(self, inst, attr, value):
239
+ try:
240
+ in_options = value in self.options
241
+ except TypeError: # e.g. `1 in "abc"`
242
+ in_options = False
243
+
244
+ if not in_options:
245
+ msg = f"'{attr.name}' must be in {self._original_options!r} (got {value!r})"
246
+ raise ValueError(
247
+ msg,
248
+ attr,
249
+ self._original_options,
250
+ value,
251
+ )
252
+
253
+ def __repr__(self):
254
+ return f"<in_ validator with options {self._original_options!r}>"
255
+
256
+
257
+ def in_(options):
258
+ """
259
+ A validator that raises a `ValueError` if the initializer is called with a
260
+ value that does not belong in the *options* provided.
261
+
262
+ The check is performed using ``value in options``, so *options* has to
263
+ support that operation.
264
+
265
+ To keep the validator hashable, dicts, lists, and sets are transparently
266
+ transformed into a `tuple`.
267
+
268
+ Args:
269
+ options: Allowed options.
270
+
271
+ Raises:
272
+ ValueError:
273
+ With a human readable error message, the attribute (of type
274
+ `attrs.Attribute`), the expected options, and the value it got.
275
+
276
+ .. versionadded:: 17.1.0
277
+ .. versionchanged:: 22.1.0
278
+ The ValueError was incomplete until now and only contained the human
279
+ readable error message. Now it contains all the information that has
280
+ been promised since 17.1.0.
281
+ .. versionchanged:: 24.1.0
282
+ *options* that are a list, dict, or a set are now transformed into a
283
+ tuple to keep the validator hashable.
284
+ """
285
+ repr_options = options
286
+ if isinstance(options, (list, dict, set)):
287
+ options = tuple(options)
288
+
289
+ return _InValidator(options, repr_options)
290
+
291
+
292
+ @attrs(repr=False, slots=False, unsafe_hash=True)
293
+ class _IsCallableValidator:
294
+ def __call__(self, inst, attr, value):
295
+ """
296
+ We use a callable class to be able to change the ``__repr__``.
297
+ """
298
+ if not callable(value):
299
+ message = (
300
+ "'{name}' must be callable "
301
+ "(got {value!r} that is a {actual!r})."
302
+ )
303
+ raise NotCallableError(
304
+ msg=message.format(
305
+ name=attr.name, value=value, actual=value.__class__
306
+ ),
307
+ value=value,
308
+ )
309
+
310
+ def __repr__(self):
311
+ return "<is_callable validator>"
312
+
313
+
314
+ def is_callable():
315
+ """
316
+ A validator that raises a `attrs.exceptions.NotCallableError` if the
317
+ initializer is called with a value for this particular attribute that is
318
+ not callable.
319
+
320
+ .. versionadded:: 19.1.0
321
+
322
+ Raises:
323
+ attrs.exceptions.NotCallableError:
324
+ With a human readable error message containing the attribute
325
+ (`attrs.Attribute`) name, and the value it got.
326
+ """
327
+ return _IsCallableValidator()
328
+
329
+
330
+ @attrs(repr=False, slots=True, unsafe_hash=True)
331
+ class _DeepIterable:
332
+ member_validator = attrib(validator=is_callable())
333
+ iterable_validator = attrib(
334
+ default=None, validator=optional(is_callable())
335
+ )
336
+
337
+ def __call__(self, inst, attr, value):
338
+ """
339
+ We use a callable class to be able to change the ``__repr__``.
340
+ """
341
+ if self.iterable_validator is not None:
342
+ self.iterable_validator(inst, attr, value)
343
+
344
+ for member in value:
345
+ self.member_validator(inst, attr, member)
346
+
347
+ def __repr__(self):
348
+ iterable_identifier = (
349
+ ""
350
+ if self.iterable_validator is None
351
+ else f" {self.iterable_validator!r}"
352
+ )
353
+ return (
354
+ f"<deep_iterable validator for{iterable_identifier}"
355
+ f" iterables of {self.member_validator!r}>"
356
+ )
357
+
358
+
359
+ def deep_iterable(member_validator, iterable_validator=None):
360
+ """
361
+ A validator that performs deep validation of an iterable.
362
+
363
+ Args:
364
+ member_validator: Validator to apply to iterable members.
365
+
366
+ iterable_validator:
367
+ Validator to apply to iterable itself (optional).
368
+
369
+ Raises
370
+ TypeError: if any sub-validators fail
371
+
372
+ .. versionadded:: 19.1.0
373
+ """
374
+ if isinstance(member_validator, (list, tuple)):
375
+ member_validator = and_(*member_validator)
376
+ return _DeepIterable(member_validator, iterable_validator)
377
+
378
+
379
+ @attrs(repr=False, slots=True, unsafe_hash=True)
380
+ class _DeepMapping:
381
+ key_validator = attrib(validator=is_callable())
382
+ value_validator = attrib(validator=is_callable())
383
+ mapping_validator = attrib(default=None, validator=optional(is_callable()))
384
+
385
+ def __call__(self, inst, attr, value):
386
+ """
387
+ We use a callable class to be able to change the ``__repr__``.
388
+ """
389
+ if self.mapping_validator is not None:
390
+ self.mapping_validator(inst, attr, value)
391
+
392
+ for key in value:
393
+ self.key_validator(inst, attr, key)
394
+ self.value_validator(inst, attr, value[key])
395
+
396
+ def __repr__(self):
397
+ return f"<deep_mapping validator for objects mapping {self.key_validator!r} to {self.value_validator!r}>"
398
+
399
+
400
+ def deep_mapping(key_validator, value_validator, mapping_validator=None):
401
+ """
402
+ A validator that performs deep validation of a dictionary.
403
+
404
+ Args:
405
+ key_validator: Validator to apply to dictionary keys.
406
+
407
+ value_validator: Validator to apply to dictionary values.
408
+
409
+ mapping_validator:
410
+ Validator to apply to top-level mapping attribute (optional).
411
+
412
+ .. versionadded:: 19.1.0
413
+
414
+ Raises:
415
+ TypeError: if any sub-validators fail
416
+ """
417
+ return _DeepMapping(key_validator, value_validator, mapping_validator)
418
+
419
+
420
+ @attrs(repr=False, frozen=True, slots=True)
421
+ class _NumberValidator:
422
+ bound = attrib()
423
+ compare_op = attrib()
424
+ compare_func = attrib()
425
+
426
+ def __call__(self, inst, attr, value):
427
+ """
428
+ We use a callable class to be able to change the ``__repr__``.
429
+ """
430
+ if not self.compare_func(value, self.bound):
431
+ msg = f"'{attr.name}' must be {self.compare_op} {self.bound}: {value}"
432
+ raise ValueError(msg)
433
+
434
+ def __repr__(self):
435
+ return f"<Validator for x {self.compare_op} {self.bound}>"
436
+
437
+
438
+ def lt(val):
439
+ """
440
+ A validator that raises `ValueError` if the initializer is called with a
441
+ number larger or equal to *val*.
442
+
443
+ The validator uses `operator.lt` to compare the values.
444
+
445
+ Args:
446
+ val: Exclusive upper bound for values.
447
+
448
+ .. versionadded:: 21.3.0
449
+ """
450
+ return _NumberValidator(val, "<", operator.lt)
451
+
452
+
453
+ def le(val):
454
+ """
455
+ A validator that raises `ValueError` if the initializer is called with a
456
+ number greater than *val*.
457
+
458
+ The validator uses `operator.le` to compare the values.
459
+
460
+ Args:
461
+ val: Inclusive upper bound for values.
462
+
463
+ .. versionadded:: 21.3.0
464
+ """
465
+ return _NumberValidator(val, "<=", operator.le)
466
+
467
+
468
+ def ge(val):
469
+ """
470
+ A validator that raises `ValueError` if the initializer is called with a
471
+ number smaller than *val*.
472
+
473
+ The validator uses `operator.ge` to compare the values.
474
+
475
+ Args:
476
+ val: Inclusive lower bound for values
477
+
478
+ .. versionadded:: 21.3.0
479
+ """
480
+ return _NumberValidator(val, ">=", operator.ge)
481
+
482
+
483
+ def gt(val):
484
+ """
485
+ A validator that raises `ValueError` if the initializer is called with a
486
+ number smaller or equal to *val*.
487
+
488
+ The validator uses `operator.ge` to compare the values.
489
+
490
+ Args:
491
+ val: Exclusive lower bound for values
492
+
493
+ .. versionadded:: 21.3.0
494
+ """
495
+ return _NumberValidator(val, ">", operator.gt)
496
+
497
+
498
+ @attrs(repr=False, frozen=True, slots=True)
499
+ class _MaxLengthValidator:
500
+ max_length = attrib()
501
+
502
+ def __call__(self, inst, attr, value):
503
+ """
504
+ We use a callable class to be able to change the ``__repr__``.
505
+ """
506
+ if len(value) > self.max_length:
507
+ msg = f"Length of '{attr.name}' must be <= {self.max_length}: {len(value)}"
508
+ raise ValueError(msg)
509
+
510
+ def __repr__(self):
511
+ return f"<max_len validator for {self.max_length}>"
512
+
513
+
514
+ def max_len(length):
515
+ """
516
+ A validator that raises `ValueError` if the initializer is called
517
+ with a string or iterable that is longer than *length*.
518
+
519
+ Args:
520
+ length (int): Maximum length of the string or iterable
521
+
522
+ .. versionadded:: 21.3.0
523
+ """
524
+ return _MaxLengthValidator(length)
525
+
526
+
527
+ @attrs(repr=False, frozen=True, slots=True)
528
+ class _MinLengthValidator:
529
+ min_length = attrib()
530
+
531
+ def __call__(self, inst, attr, value):
532
+ """
533
+ We use a callable class to be able to change the ``__repr__``.
534
+ """
535
+ if len(value) < self.min_length:
536
+ msg = f"Length of '{attr.name}' must be >= {self.min_length}: {len(value)}"
537
+ raise ValueError(msg)
538
+
539
+ def __repr__(self):
540
+ return f"<min_len validator for {self.min_length}>"
541
+
542
+
543
+ def min_len(length):
544
+ """
545
+ A validator that raises `ValueError` if the initializer is called
546
+ with a string or iterable that is shorter than *length*.
547
+
548
+ Args:
549
+ length (int): Minimum length of the string or iterable
550
+
551
+ .. versionadded:: 22.1.0
552
+ """
553
+ return _MinLengthValidator(length)
554
+
555
+
556
+ @attrs(repr=False, slots=True, unsafe_hash=True)
557
+ class _SubclassOfValidator:
558
+ type = attrib()
559
+
560
+ def __call__(self, inst, attr, value):
561
+ """
562
+ We use a callable class to be able to change the ``__repr__``.
563
+ """
564
+ if not issubclass(value, self.type):
565
+ msg = f"'{attr.name}' must be a subclass of {self.type!r} (got {value!r})."
566
+ raise TypeError(
567
+ msg,
568
+ attr,
569
+ self.type,
570
+ value,
571
+ )
572
+
573
+ def __repr__(self):
574
+ return f"<subclass_of validator for type {self.type!r}>"
575
+
576
+
577
+ def _subclass_of(type):
578
+ """
579
+ A validator that raises a `TypeError` if the initializer is called with a
580
+ wrong type for this particular attribute (checks are performed using
581
+ `issubclass` therefore it's also valid to pass a tuple of types).
582
+
583
+ Args:
584
+ type (type | tuple[type, ...]): The type(s) to check for.
585
+
586
+ Raises:
587
+ TypeError:
588
+ With a human readable error message, the attribute (of type
589
+ `attrs.Attribute`), the expected type, and the value it got.
590
+ """
591
+ return _SubclassOfValidator(type)
592
+
593
+
594
+ @attrs(repr=False, slots=True, unsafe_hash=True)
595
+ class _NotValidator:
596
+ validator = attrib()
597
+ msg = attrib(
598
+ converter=default_if_none(
599
+ "not_ validator child '{validator!r}' "
600
+ "did not raise a captured error"
601
+ )
602
+ )
603
+ exc_types = attrib(
604
+ validator=deep_iterable(
605
+ member_validator=_subclass_of(Exception),
606
+ iterable_validator=instance_of(tuple),
607
+ ),
608
+ )
609
+
610
+ def __call__(self, inst, attr, value):
611
+ try:
612
+ self.validator(inst, attr, value)
613
+ except self.exc_types:
614
+ pass # suppress error to invert validity
615
+ else:
616
+ raise ValueError(
617
+ self.msg.format(
618
+ validator=self.validator,
619
+ exc_types=self.exc_types,
620
+ ),
621
+ attr,
622
+ self.validator,
623
+ value,
624
+ self.exc_types,
625
+ )
626
+
627
+ def __repr__(self):
628
+ return f"<not_ validator wrapping {self.validator!r}, capturing {self.exc_types!r}>"
629
+
630
+
631
+ def not_(validator, *, msg=None, exc_types=(ValueError, TypeError)):
632
+ """
633
+ A validator that wraps and logically 'inverts' the validator passed to it.
634
+ It will raise a `ValueError` if the provided validator *doesn't* raise a
635
+ `ValueError` or `TypeError` (by default), and will suppress the exception
636
+ if the provided validator *does*.
637
+
638
+ Intended to be used with existing validators to compose logic without
639
+ needing to create inverted variants, for example, ``not_(in_(...))``.
640
+
641
+ Args:
642
+ validator: A validator to be logically inverted.
643
+
644
+ msg (str):
645
+ Message to raise if validator fails. Formatted with keys
646
+ ``exc_types`` and ``validator``.
647
+
648
+ exc_types (tuple[type, ...]):
649
+ Exception type(s) to capture. Other types raised by child
650
+ validators will not be intercepted and pass through.
651
+
652
+ Raises:
653
+ ValueError:
654
+ With a human readable error message, the attribute (of type
655
+ `attrs.Attribute`), the validator that failed to raise an
656
+ exception, the value it got, and the expected exception types.
657
+
658
+ .. versionadded:: 22.2.0
659
+ """
660
+ try:
661
+ exc_types = tuple(exc_types)
662
+ except TypeError:
663
+ exc_types = (exc_types,)
664
+ return _NotValidator(validator, msg, exc_types)
665
+
666
+
667
+ @attrs(repr=False, slots=True, unsafe_hash=True)
668
+ class _OrValidator:
669
+ validators = attrib()
670
+
671
+ def __call__(self, inst, attr, value):
672
+ for v in self.validators:
673
+ try:
674
+ v(inst, attr, value)
675
+ except Exception: # noqa: BLE001, PERF203, S112
676
+ continue
677
+ else:
678
+ return
679
+
680
+ msg = f"None of {self.validators!r} satisfied for value {value!r}"
681
+ raise ValueError(msg)
682
+
683
+ def __repr__(self):
684
+ return f"<or validator wrapping {self.validators!r}>"
685
+
686
+
687
+ def or_(*validators):
688
+ """
689
+ A validator that composes multiple validators into one.
690
+
691
+ When called on a value, it runs all wrapped validators until one of them is
692
+ satisfied.
693
+
694
+ Args:
695
+ validators (~collections.abc.Iterable[typing.Callable]):
696
+ Arbitrary number of validators.
697
+
698
+ Raises:
699
+ ValueError:
700
+ If no validator is satisfied. Raised with a human-readable error
701
+ message listing all the wrapped validators and the value that
702
+ failed all of them.
703
+
704
+ .. versionadded:: 24.1.0
705
+ """
706
+ vals = []
707
+ for v in validators:
708
+ vals.extend(v.validators if isinstance(v, _OrValidator) else [v])
709
+
710
+ return _OrValidator(tuple(vals))
venv/lib/python3.10/site-packages/attr/validators.pyi ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from types import UnionType
2
+ from typing import (
3
+ Any,
4
+ AnyStr,
5
+ Callable,
6
+ Container,
7
+ ContextManager,
8
+ Iterable,
9
+ Mapping,
10
+ Match,
11
+ Pattern,
12
+ TypeVar,
13
+ overload,
14
+ )
15
+
16
+ from attrs import _ValidatorType
17
+ from attrs import _ValidatorArgType
18
+
19
+ _T = TypeVar("_T")
20
+ _T1 = TypeVar("_T1")
21
+ _T2 = TypeVar("_T2")
22
+ _T3 = TypeVar("_T3")
23
+ _I = TypeVar("_I", bound=Iterable)
24
+ _K = TypeVar("_K")
25
+ _V = TypeVar("_V")
26
+ _M = TypeVar("_M", bound=Mapping)
27
+
28
+ def set_disabled(run: bool) -> None: ...
29
+ def get_disabled() -> bool: ...
30
+ def disabled() -> ContextManager[None]: ...
31
+
32
+ # To be more precise on instance_of use some overloads.
33
+ # If there are more than 3 items in the tuple then we fall back to Any
34
+ @overload
35
+ def instance_of(type: type[_T]) -> _ValidatorType[_T]: ...
36
+ @overload
37
+ def instance_of(type: tuple[type[_T]]) -> _ValidatorType[_T]: ...
38
+ @overload
39
+ def instance_of(
40
+ type: tuple[type[_T1], type[_T2]],
41
+ ) -> _ValidatorType[_T1 | _T2]: ...
42
+ @overload
43
+ def instance_of(
44
+ type: tuple[type[_T1], type[_T2], type[_T3]],
45
+ ) -> _ValidatorType[_T1 | _T2 | _T3]: ...
46
+ @overload
47
+ def instance_of(type: tuple[type, ...]) -> _ValidatorType[Any]: ...
48
+ @overload
49
+ def instance_of(type: UnionType) -> _ValidatorType[Any]: ...
50
+ def optional(
51
+ validator: (
52
+ _ValidatorType[_T]
53
+ | list[_ValidatorType[_T]]
54
+ | tuple[_ValidatorType[_T]]
55
+ ),
56
+ ) -> _ValidatorType[_T | None]: ...
57
+ def in_(options: Container[_T]) -> _ValidatorType[_T]: ...
58
+ def and_(*validators: _ValidatorType[_T]) -> _ValidatorType[_T]: ...
59
+ def matches_re(
60
+ regex: Pattern[AnyStr] | AnyStr,
61
+ flags: int = ...,
62
+ func: Callable[[AnyStr, AnyStr, int], Match[AnyStr] | None] | None = ...,
63
+ ) -> _ValidatorType[AnyStr]: ...
64
+ def deep_iterable(
65
+ member_validator: _ValidatorArgType[_T],
66
+ iterable_validator: _ValidatorType[_I] | None = ...,
67
+ ) -> _ValidatorType[_I]: ...
68
+ def deep_mapping(
69
+ key_validator: _ValidatorType[_K],
70
+ value_validator: _ValidatorType[_V],
71
+ mapping_validator: _ValidatorType[_M] | None = ...,
72
+ ) -> _ValidatorType[_M]: ...
73
+ def is_callable() -> _ValidatorType[_T]: ...
74
+ def lt(val: _T) -> _ValidatorType[_T]: ...
75
+ def le(val: _T) -> _ValidatorType[_T]: ...
76
+ def ge(val: _T) -> _ValidatorType[_T]: ...
77
+ def gt(val: _T) -> _ValidatorType[_T]: ...
78
+ def max_len(length: int) -> _ValidatorType[_T]: ...
79
+ def min_len(length: int) -> _ValidatorType[_T]: ...
80
+ def not_(
81
+ validator: _ValidatorType[_T],
82
+ *,
83
+ msg: str | None = None,
84
+ exc_types: type[Exception] | Iterable[type[Exception]] = ...,
85
+ ) -> _ValidatorType[_T]: ...
86
+ def or_(*validators: _ValidatorType[_T]) -> _ValidatorType[_T]: ...
venv/lib/python3.10/site-packages/glom/__init__.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from glom.core import (glom,
2
+ Fill,
3
+ Auto,
4
+ register,
5
+ register_op,
6
+ Glommer,
7
+ Call,
8
+ Invoke,
9
+ Spec,
10
+ Ref,
11
+ OMIT, # backwards compat
12
+ SKIP,
13
+ STOP,
14
+ UP,
15
+ ROOT,
16
+ MODE,
17
+ Path,
18
+ Vars,
19
+ Val,
20
+ Literal, # backwards compat 2020-07
21
+ Let, # backwards compat 2020-07
22
+ Coalesce,
23
+ Inspect,
24
+ Pipe,
25
+ GlomError,
26
+ BadSpec,
27
+ PathAccessError,
28
+ PathAssignError,
29
+ CoalesceError,
30
+ UnregisteredTarget,
31
+ T, S, A)
32
+
33
+ from glom.reduction import Sum, Fold, Flatten, flatten, FoldError, Merge, merge
34
+ from glom.matching import (M,
35
+ Or,
36
+ And,
37
+ Not,
38
+ Match,
39
+ MatchError,
40
+ TypeMatchError,
41
+ Regex,
42
+ Optional,
43
+ Required,
44
+ Switch,
45
+ Check,
46
+ CheckError)
47
+ from glom.mutation import Assign, Delete, assign, delete, PathDeleteError
48
+
49
+ # there's no -ion word that really fits what "streaming" means.
50
+ # generation, production, iteration, all have more relevant meanings
51
+ # elsewhere. (maybe procrastination :P)
52
+ from glom.streaming import Iter
53
+
54
+ from glom._version import __version__
venv/lib/python3.10/site-packages/glom/__main__.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from glom.cli import console_main
2
+
3
+ if __name__ == '__main__':
4
+ console_main()