Skip to content

Commit 2315d00

Browse files
committed
typo in docstring
1 parent 81d94fc commit 2315d00

4 files changed

Lines changed: 283 additions & 275 deletions

File tree

hunterMakesPy/coping.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ class PackageSettings:
3636
package identifiers and installation paths if they are not passed to the `class` constructor. Python `dataclasses` are easy to
3737
subtype and extend.
3838
39-
Parameters
39+
Attributes
4040
----------
4141
identifierPackageFALLBACK : str = ''
4242
Fallback package identifier used only during initialization when automatic discovery fails.
43-
pathPackage : Path = Path()
44-
Absolute path to the installed package directory. Automatically resolved from `identifierPackage` if not provided.
4543
identifierPackage : str = ''
46-
Canonical name of the package. Automatically extracted from `pyproject.toml`.
44+
Canonical name of the package. Automatically extracted from "pyproject.toml".
45+
pathPackage : Path = getPathPackageINSTALLING(identifierPackage)
46+
Absolute path to the installed package directory. Automatically resolved from `identifierPackage` if not provided.
4747
fileExtension : str = '.py'
4848
Default file extension.
4949
@@ -85,34 +85,39 @@ def __post_init__(self, identifierPackageFALLBACK: str) -> None:
8585
if self.pathPackage == Path() and self.identifierPackage:
8686
self.pathPackage = getPathPackageINSTALLING(self.identifierPackage)
8787

88-
def raiseIfNone(returnTarget: TypeSansNone | None, errorMessage: str | None = None) -> TypeSansNone:
89-
"""Raise a `ValueError` if the target value is `None`, otherwise return the value: tell the type checker that the return value is not `None`.
90-
91-
(AI generated docstring)
88+
def raiseIfNone(expression: TypeSansNone | None, errorMessage: str | None = None) -> TypeSansNone:
89+
"""Convert the `expression` return annotation from '`cerPytainty | None`' to '`cerPytainty`' because `expression` cannot be `None`; `raise` an `Exception` if you're wrong.
9290
93-
This is a defensive programming function that converts unexpected `None` values into explicit errors with context. It is useful for asserting that functions that might return `None` have actually returned a meaningful value.
91+
The Python interpreter evaluates `expression` to a value: think of a function call or an attribute access. You can use
92+
`raiseIfNone` for fail early defensive programming. I use it, however, to cure type-checker-nihilism: that's when "or `None`"
93+
return types cause your type checker to repeatedly say, "You can't do that because the value might be `None`."
9494
9595
Parameters
9696
----------
97-
returnTarget : TypeSansNone | None
98-
The value to check for `None`. If not `None`, this value is returned unchanged.
99-
errorMessage : str | None = None
100-
Custom error message to include in the `ValueError`. If `None`, a default message with debugging hints is used.
97+
expression : TypeSansNone | None
98+
Python code with a return type that is a `union` of `None` and `TypeSansNone`, which is a stand-in for one or more other types.
99+
errorMessage : str | None = 'A function unexpectedly returned `None`. Hint: look at the traceback immediately before `raiseIfNone`.'
100+
Custom error message for the `ValueError` `Exception` if `expression` is `None`.
101101
102102
Returns
103103
-------
104-
returnTarget : TypeSansNone
105-
The original `returnTarget` value, guaranteed to not be `None`.
104+
contentment : TypeSansNone
105+
The value returned by `expression`, but guaranteed to not be `None`.
106106
107107
Raises
108108
------
109109
ValueError
110-
If `returnTarget` is `None`.
110+
If the value returned by `expression` is `None`.
111111
112112
Examples
113113
--------
114-
Ensure a function result is not `None`:
114+
Basic usage with attribute access:
115+
```python
116+
annotation = raiseIfNone(ast_arg.annotation)
117+
# Raises ValueError if ast_arg.annotation is None
118+
```
115119
120+
Function return value validation:
116121
```python
117122
def findFirstMatch(listItems: list[str], pattern: str) -> str | None:
118123
for item in listItems:
@@ -122,28 +127,27 @@ def findFirstMatch(listItems: list[str], pattern: str) -> str | None:
122127
123128
listFiles = ['document.txt', 'image.png', 'data.csv']
124129
filename = raiseIfNone(findFirstMatch(listFiles, '.txt'))
125-
# Returns 'document.txt'
130+
# Returns 'document.txt' when match exists
126131
```
127132
128-
Handle dictionary lookups with custom error messages:
129-
133+
Dictionary value retrieval with custom message:
130134
```python
131135
configurationMapping = {'host': 'localhost', 'port': 8080}
132136
host = raiseIfNone(configurationMapping.get('host'),
133137
"Configuration must include 'host' setting")
134-
# Returns 'localhost'
138+
# Returns 'localhost' when key exists
135139
136140
# This would raise ValueError with custom message:
137141
# database = raiseIfNone(configurationMapping.get('database'),
138-
# "Configuration must include 'database' setting")
142+
# "Configuration must include 'database' setting")
139143
```
140144
141145
Thanks
142146
------
143-
sobolevn, https://github.com/sobolevn, for the seed of the function. https://github.com/python/typing/discussions/1997#discussioncomment-13108399
147+
sobolevn, https://github.com/sobolevn, for the seed of this function. https://github.com/python/typing/discussions/1997#discussioncomment-13108399
144148
145149
"""
146-
if returnTarget is None:
150+
if expression is None:
147151
message = errorMessage or 'A function unexpectedly returned `None`. Hint: look at the traceback immediately before `raiseIfNone`.'
148152
raise ValueError(message)
149-
return returnTarget
153+
return expression

0 commit comments

Comments
 (0)