Skip to content

Commit afaf052

Browse files
fix: Get rid of invalid NoneType value error message.
1 parent fbe3499 commit afaf052

6 files changed

Lines changed: 70 additions & 38 deletions

File tree

CHANGELOG.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,47 @@ Help this project by [Donation](DONATE.md)
66
Changes
77
-------
88

9+
### v3.3.1
10+
11+
Get rid of `invalid NoneType value` error message.
12+
13+
In the previous versions, if you used an optional union type such as `int | float | None`
14+
which ended with `None`, you'd get an error saying `invalid NoneType value` that didn't
15+
make any sense to the user. The code has been updated to use the name of the last
16+
non-None type for the error message in these situations.
17+
18+
#### v3.3.1 update demonstration
19+
20+
```python
21+
import log21
22+
from log21.helper_types import FileSize
23+
24+
25+
def main(min_size: FileSize | None = None, max_size: FileSize | None = None) -> None:
26+
log21.info("Min Size: %s, Max Size: %s", args=(min_size, max_size))
27+
28+
if __name__ == "__main__":
29+
log21.argumentify(main)
30+
```
31+
32+
Before v3.3.1:
33+
34+
```shell
35+
$ python test.py -m Hello
36+
usage: test.py [-h] [--min-size MIN_SIZE] [--max-size MAX_SIZE]
37+
38+
test.py: error: argument --min-size/-m: invalid NoneType value: 'Hello'
39+
```
40+
41+
With v3.3.1 update:
42+
43+
```shell
44+
$ python test.py -m Hello
45+
usage: test.py [-h] [--min-size MIN_SIZE] [--max-size MAX_SIZE]
46+
47+
test.py: error: argument --min-size/-m: invalid FileSize value: 'Hello'
48+
```
49+
950
### v3.3.0
1051

1152
Add `log21.helper_types` module.

README.md

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,55 +69,45 @@ pip install git+https://github.com/MPCodeWriter21/log21
6969
Changelog
7070
---------
7171

72-
### v3.3.0
72+
### v3.3.1
7373

74-
Add `log21.helper_types` module.
74+
Get rid of `invalid NoneType value` error message.
7575

76-
This module contains a collection of useful types meant for using with argument parser
77-
to parse CLI arguments to more usable formats.
78-
79-
+ `FileSize`: Can take `str` and `int` values. Will convert human inputs such as "121 KB",
80-
"21MiB", or "4.56 GB" to bytes. Can also be used to represent bytes value in more
81-
human-readable formats.
82-
83-
For even more control you can still define Logger, Handlers, and Formatters manually.
76+
In the previous versions, if you used an optional union type such as `int | float | None`
77+
which ended with `None`, you'd get an error saying `invalid NoneType value` that didn't
78+
make any sense to the user. The code has been updated to use the name of the last
79+
non-None type for the error message in these situations.
8480

8581
#### Example
8682

8783
```python
88-
from pathlib import Path
89-
9084
import log21
9185
from log21.helper_types import FileSize
9286

9387

94-
def main(path: Path, min_size: FileSize, max_size: FileSize, /):
95-
log21.info(
96-
"Files that are smaller than %s or bigger than %s will be ignored.",
97-
args=(min_size, max_size),
98-
)
99-
100-
for file in path.iterdir():
101-
if not file.is_file():
102-
continue
103-
if min_size <= (size := file.stat().st_size) <= max_size:
104-
log21.print(
105-
"`%s` is %s.",
106-
args=(file, FileSize(size).humanize(binary=False, fmt="%.4f")),
107-
)
108-
88+
def main(min_size: FileSize | None = None, max_size: FileSize | None = None) -> None:
89+
log21.info("Min Size: %s, Max Size: %s", args=(min_size, max_size))
10990

11091
if __name__ == "__main__":
11192
log21.argumentify(main)
11293
```
11394

114-
Example usage and output:
95+
Before v3.3.1:
11596

11697
```shell
117-
$ uv run test.py . "1.23MiB" "0.5 GB"
118-
[21:21:21] [INFO] Files that are smaller than 1.23 MiB or bigger than 476.84 MiB will be
119-
ignored.
120-
`myfile21.zip` is 35.1856 MB.
98+
$ python test.py -m Hello
99+
usage: test.py [-h] [--min-size MIN_SIZE] [--max-size MAX_SIZE]
100+
101+
test.py: error: argument --min-size/-m: invalid NoneType value: 'Hello'
102+
```
103+
104+
With v3.3.1 update:
105+
106+
```shell
107+
$ python test.py -m Hello
108+
usage: test.py [-h] [--min-size MIN_SIZE] [--max-size MAX_SIZE]
109+
110+
test.py: error: argument --min-size/-m: invalid FileSize value: 'Hello'
121111
```
122112

123113
[Full CHANGELOG](https://github.com/MPCodeWriter21/log21/blob/master/CHANGELOG.md)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ dependencies = [
2323
"webcolors",
2424
"docstring-parser"
2525
]
26-
version = "3.3.0"
26+
version = "3.3.1"
2727

2828
[build-system]
2929
requires = ["uv_build>=0.8.15,<0.9.0"]

src/log21/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
# yapf: enable
3434

3535
__author__ = 'CodeWriter21 (Mehrad Pooryoussof)'
36-
__version__ = '3.3.0'
36+
__version__ = '3.3.1'
3737
__github__ = 'https://GitHub.com/MPCodeWriter21/log21'
3838
__all__ = [
3939
'ColorizingStreamHandler', 'DecolorizingFileHandler', 'ColorizingFormatter',

src/log21/argparse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,8 @@ def _get_value(self, action, arg_string): # noqa: ANN202
784784
else:
785785
exception = ValueError()
786786
for type_ in func_type:
787-
name = getattr(type_, '__name__', repr(type_))
787+
if type_ is not type(None):
788+
name = getattr(type_, '__name__', repr(type_))
788789
try:
789790
result = type_(arg_string)
790791
break

src/log21/argumentify.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ def _argumentify(functions: _Dict[str, Callable]) -> None:
451451
args = []
452452
kwargs = {}
453453
info = None
454-
for name, (function, info) in functions_info.items(): # noqa: B007
454+
for _name, (function, info) in functions_info.items(): # noqa: B007
455455
if function == cli_args.func:
456456
break
457457
else:
@@ -475,7 +475,7 @@ def _argumentify(functions: _Dict[str, Callable]) -> None:
475475

476476
def argumentify(
477477
entry_point: _Union[Callable, _List[Callable], _Dict[str, Callable]]
478-
) -> _Callable:
478+
) -> _Union[Callable, _List[Callable], _Dict[str, Callable]]:
479479
"""This function argumentifies one or more functions as the entry point of the
480480
script.
481481
@@ -493,7 +493,7 @@ def argumentify(
493493
12 if __name__ == '__main__':
494494
13 argumentify(main)
495495
496-
$ python argumentified.py Ahmad Ahmadi --age 20
496+
$ python argumentified.py Ahmad Mohammadi --age 20
497497
Ahmad Ahmadi is 20 years old.
498498
$ python argumentified.py Mehrad Pooryoussof
499499
Mehrad Pooryoussof is not yet born.

0 commit comments

Comments
 (0)