-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Annotation for getLevelName in logging does not accept str
but the code does
#1842
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
Comments
The docs at
https://docs.python.org/3/library/logging.html#logging.getLevelName imply
that the "reverse" behavior (where it returns an int when a string is
passed in) is considered a mistake and undocumented:
*Changed in version 3.4:* In Python versions earlier than 3.4, this
function could also be passed a text level, and would return the
corresponding numeric value of the level. This undocumented behaviour was
considered a mistake, and was removed in Python 3.4, but reinstated in
3.4.2 due to retain backward compatibility.
I think it's a feature that typeshed doesn't repeat this mistake -- if you
find this bug in your code thanks to static typing you should be grateful,
not request a change to typeshed. :-)
|
Thanks - since the "reverse" behavior is considered a bug, typeshed is definitely both correct and helpful here. I think the fundamental problem, though, is that it is difficult for the user (especially someone new to Python) to determine whether or not the reverse behavior in fact is a bug. There are many examples of the deprecated behavior on the web, the code supports the reverse behavior without a comment that its use is deprecated, and the docstring doesn't contain the "Changed in version 3.4... message" (only the RST docs do, and many inexperienced users would be unaware they need to check both places - you wouldn't for the Java Standard Library, for example). There is a comment All of that isn't typeshed's problem, of course - is https://bugs.python.org/ the correct place to request such an update to the docstring to clarify the reverse behavior is deprecated? |
Yeah, if you really think this requires action, bugs.python.org is the
place to request it.
|
To better reflect the implementation's behaviour, python#2730 changed `logging.getLevelName` to accept `int | str` and return `Any` (the latter due to the need to avoid union return types). However, this isn't ideal if you're passing in an `int`, in which case the implementation always returns a `str`. Add overloads for this. This is all arguably a bit unfortunate in light of python#1842 (comment), but I don't want to relitigate that here. I've at least left a comment.
Currently for
getLevelName
inlogging
there isHowever the code for it (Python 3.6.3) accepts a string argument as well:
It is not clear to me from the method's docstring that the behavior of the code with respect to strings is guaranteed, though, so perhaps leaving it as
int
is safer.The text was updated successfully, but these errors were encountered: