-
Notifications
You must be signed in to change notification settings - Fork 23
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
Accept math
constants in defaults
#355
Conversation
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
I don't think it needs to be nearly this complicated, though. If you just add these constants to the _ALLOWED_ATTRIBUTES_IN_DEFAULTS
frozenset, I don't think you'll need to make any other changes to pyi.py
I think $ echo 'import math; x = -math.inf' | python3 -m ast
Module(
body=[
Import(
names=[
alias(name='math')]),
Assign(
targets=[
Name(id='x', ctx=Store())],
value=UnaryOp(
op=USub(),
operand=Attribute(
value=Name(id='math', ctx=Load()),
attr='inf',
ctx=Load())))],
type_ignores=[]) The AST for |
Ah, I see. That requires a slightly more invasive change to the code than I was expecting, but I suppose it's reasonable. |
This comment has been minimized.
This comment has been minimized.
Could you add a CHANGELOG entry? This file here: https://github.com/PyCQA/flake8-pyi/blob/main/CHANGELOG.md |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few changelog nitpicks :)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
pyi.py
Outdated
@@ -695,6 +695,10 @@ def _analyse_union(members: Sequence[ast.expr]) -> UnionAnalysis: | |||
) | |||
|
|||
|
|||
_ALLOWED_MATH_ATTRIBUTES_IN_DEFAULTS = frozenset( | |||
{"math.inf", "math.e", "math.pi", "math.tau"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This set is small enough that we could probably actually just inline it in the _is_valid_math_constant
function. But on the other hand, I guess having it as a global constant like this maybe makes the code more extensible, if we want to add this to this set in the future. No strong opinion either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the function will be called multiple times, caching the frozenset as a global variable will somehow gain some speed. And also, it would be extensible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I'll wait a little bit before merging in case any other maintainers have objections :)
This comment has been minimized.
This comment has been minimized.
This change has no effect on typeshed. 🤖🎉 |
Thanks @XuehaiPan! |
Allow
math.inf
,math.e
,math.pi
,math.tau
, and their negatives in defaults.Reject:
float('inf')
/float('-inf')
: function callsmath.nan
: not constantmath.nan == math.nan -> False
Resolves #354