-
-
Notifications
You must be signed in to change notification settings - Fork 323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't use mutable objects as default arguments #161
Comments
As you point out, this usage is safe because the functions in question don't mutate, return, or expose the value of the argument. However, for the avoidance of confusion I agree that it would be great to make this argument immutable with |
Yes, I can add a PR. Is there a reason you suggested |
Good point, we can use strings directly as the argument values. For legibility, I would like to keep the value directly in the signature instead of setting it to None. |
Fixes #161 Avoid using mutable objects as default arguments as functions. Even though this particular usage was safe, it opens up the projects to issues down the road if the default argument is mutated within the function. For this usage, we don't need an actual `list` object, just a sequence of strings, so we can use a string itself.
pyotp.random_base32
andpyotp.random_hex
uselist("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567")
andlist("ABCDEF0123456789")
respectively as default arguments. While it does appear that this specific usage is safe, using mutable objects as default arguments is a recipe for disaster since the objects will be instantiated at module load and the reference will be shared between any calls to that module.Consider the following dangerous usage where calling
foo
prints a different result every time:From the looks of it, there is no reason these arguments need to be lists. They exist only to be passed to
random.choice
and are typed asSequence[str]
which would be satisfied by simply using the strings"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
and"ABCDEF0123456789"
directly as default arguments. And if they do need to belist
objects, they should have a default value ofNone
with a conditional check that assigns the variable within the function body, e.g.The text was updated successfully, but these errors were encountered: