-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.md.tpl
165 lines (124 loc) · 4.65 KB
/
README.md.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# python-none-objects
[![PyPI-version-badge]][PyPI-package-page]
[![Downloads-badge]][PyPIStats-package-page]
[![Code-style:black:badge]][Black-GitHub.com]
[![Imports:isort:badge]][Isort-GitHub.io]
[![Typecheck:mypy:badge]][Typecheck-mypy-lang.org]
[![Linting:pylint:badge]][Pylint-GitHub.com]
[![CodeFactor-badge]][CodeFactor-package-page]
[![CodeClimateMaintainability-badge]][CodeClimateM13y-package-page]
[![Codacy-badge]][Codacy-package-page]
![GitHub-top-language-badge]
![GitHub-license-badge]
![PyPI-python-version-badge]
![GitHub-code-size-in-bytes-badge]
| **A collection of "None" objects** |
|:----------------------------------------:|
| **compatible with various Python types** |
The following code yields warning
for "Default argument value is mutable".
```python3
from typing import List, Dict
def foo(some: int, other: List = [], thing: Dict = {}):
for o in other:
bar(some, o, thing)
```
It is usually recommended to use None instead
(<https://stackoverflow.com/questions/41686829>
/why-does-pycharm-warn-about-mutable-default-arguments-
how-can-i-work-around-the):
```python3
from typing import List, Dict, Optional
def foo(
some: int,
other: Optional[List] = None,
thing: Optional[Dict] = None,
):
if other is None:
other = []
if thing is None:
thing = {}
for o in other:
bar(some, o, thing)
```
But I prefer less boilerplate code like this:
```python3
from typing import Iterable, Mapping
from types import MappingProxyType
def foo(
some: int,
other: Iterable = (),
thing: Mapping = MappingProxyType({}),
):
for o in other:
bar(some, o, thing)
```
This package introduces constants to make the code more readable:
```python3
from typing import Iterable, Mapping
from python_none_objects import NoneIterable, NoneMapping
def foo(
some: int,
other: Iterable = NoneIterable,
thing: Mapping = NoneMapping,
):
for o in other:
bar(some, o, thing)
```
Be sure to look at the discussions on GitHub:
<https://github.com/LLyaudet/python-none-objects/discussions>.
There is a poll on the naming convention you would prefer:
<https://github.com/LLyaudet/python-none-objects/discussions/2>.
And there is a discussion on various ideas
to optimize the code with these constants:
<https://github.com/LLyaudet/python-none-objects/discussions/3>.
I think it would be better to have this kind of constants
in the standard library.
If you think after reading everything, that it is indeed a good idea,
add a star to this repository to let the rest
of the Python community
know that you would like to see such constant objects
in the language :).
<https://github.com/LLyaudet/python-none-objects/>
If the project gains popularity, I'll try to propose it officially.
[PyPI-version-badge]: https://img.shields.io/pypi/v/\
python-none-objects.svg
[PyPI-package-page]: https://pypi.org/project/\
python-none-objects/
[Downloads-badge]: https://img.shields.io/pypi/dm/\
python-none-objects
[PyPIStats-package-page]: https://pypistats.org/packages/\
python-none-objects
[Code-style:black:badge]: https://img.shields.io/badge/\
code%20style-black-000000.svg
[Black-GitHub.com]: https://github.com/psf/black
[Imports:isort:badge]: https://img.shields.io/badge/\
%20imports-isort-%231674b1?style=flat&labelColor=ef8336
[Isort-GitHub.io]: https://pycqa.github.io/isort/
[Typecheck:mypy:badge]: https://www.mypy-lang.org/static/\
mypy_badge.svg
[Typecheck-mypy-lang.org]: https://mypy-lang.org/
[Linting:pylint:badge]: https://img.shields.io/badge/\
linting-pylint-yellowgreen
[Pylint-GitHub.com]: https://github.com/pylint-dev/pylint
[CodeFactor-badge]: https://www.codefactor.io/repository/github/\
llyaudet/python-none-objects/badge
[CodeFactor-package-page]: https://www.codefactor.io/repository/\
github/llyaudet/python-none-objects
[CodeClimateMaintainability-badge]: https://api.codeclimate.com/v1/\
badges/266efb337cabd7d7941e/maintainability
[CodeClimateM13y-package-page]: https://codeclimate.com/github/\
LLyaudet/python-none-objects/maintainability
[Codacy-badge]: https://app.codacy.com/project/badge/Grade/\
4be488463e31459bb2ba02794091610d
[Codacy-package-page]: https://app.codacy.com/gh/LLyaudet/\
python-none-objects/dashboard?utm_source=gh\
&utm_medium=referral&utm_content=&utm_campaign=Badge_grade
[GitHub-top-language-badge]: https://img.shields.io/github/\
languages/top/llyaudet/python-none-objects
[GitHub-license-badge]: https://img.shields.io/github/license/\
llyaudet/python-none-objects
[PyPI-python-version-badge]: https://img.shields.io/pypi/pyversions/\
python-none-objects
[GitHub-code-size-in-bytes-badge]: https://img.shields.io/github/\
languages/code-size/llyaudet/python-none-objects