- Make sure mypy --strict passes for these two folders
uv run mypy --strict src/wcgw src/wcgw_cli
. - Use
list
directly for typing likelist[str]
no need to importList
. Same thing fortuple
,set
, etc. - No optional parameters in a function with default values. All parameters must be passed by a caller.
- This library uses
uv
as package manager. To add a packageuv add numpy
. To run pytestuv run pytest
and so on.
- Don't introduce any state unless really necessary.
- If anything can be derived, avoid storing it or passing it.
- Exception thrown inside functions are their hidden extra state which should be avoided.
- Parse don't validate: avoid throwing validation errors by letting the types avoid bad values to be passed in the first place.
- No hidden contracts and assumptions.
- Don't assume any relationship between two states unless it's encoded in the type of the state.
- Any contract should be enforced by the way types are constructed.
- If it's just not possible due to complexity to type in such a way to avoid hidden contract, add in docstring details.
-
When you can't avoid it, instead of enforcing the hidden contract as hard failure during runtime, try to return some sensible value instead. Example In PIL adding boxes outside image bounds don't do anything, but they don't fail either, making it a cleaner experience to deal with edge cases.
-
A functions signature (along with types) should be enough to understand its purpose.
-
This can be achieved by typing the parameters to only take narrow types
- Avoid mutating mutable input parameters, instead return newly derived values in the output and leave upto the caller to update the state if required.
- It should be clear from function signature what the function computes, this should also enforce the previous point of not updating mutable input parameters.