Skip to content

MNT: Enforce ruff/flake8-bugbear rules (B) #28765

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

DimitriPapadopoulos
Copy link
Contributor

No description provided.

Copy link
Member

@jorenham jorenham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the feeling that there are some threading subtleties that we might be missing here. Perhaps @ngoldbaum could also take a look at this?

@DimitriPapadopoulos
Copy link
Contributor Author

Indeed, the underlying issue remains. I'm not familiar with ContextVar and cannot find a simple way to fix it. See Factory for contextvar default value.

@ngoldbaum
Copy link
Member

I’ll try to take a look at this next week.

@charris
Copy link
Member

charris commented Apr 25, 2025

Needs a rebase.

@jorenham
Copy link
Member

this needs a rebase

Comment on lines +563 to +569
module_names = args.module_names + [
OTHER_MODULE_DOCS[name]
for name in args.module_names
if name in OTHER_MODULE_DOCS
]
# remove duplicates while maintaining order
module_names = list(dict.fromkeys(module_names))
Copy link
Member

@jorenham jorenham Apr 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically speaking, this is a bit different now, because this also de-duplicates args.module_names, which didn't happen before. I can't imagine why that would be a problem, but I figured it could be worth noting 🤷🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be missing something, but I don't think it de-duplicates args.module_names. The code is equivalent to:

>>> args_module_names = [ 1, 1, 2, 2, 3, 3 ]
>>> module_names = args_module_names + [ x for x in args_module_names if x != 2 ]
>>> 
>>> args_module_names
[1, 1, 2, 2, 3, 3]
>>> module_names
[1, 1, 2, 2, 3, 3, 1, 1, 3, 3]
>>> 
>>> module_names = list(dict.fromkeys(module_names))
>>> 
>>> args_module_names
[1, 1, 2, 2, 3, 3]
>>> module_names
[1, 2, 3]
>>> 

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

args.module_names is part of module_names, and module_names is what's being de-duped as a whole

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is args.module_names part of module_names? I don't get it, module_names is a whole new list containing values copied from args.module_names. See my example.

Copy link
Member

@jorenham jorenham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's nothing left for me to complain about 👌🏻. So unless @ngoldbaum has any objections, this should be good to merge.

DimitriPapadopoulos and others added 16 commits April 28, 2025 19:10
Using `hasattr(x, "__call__")` to test if x is callable is unreliable.
Use `callable(x)` for consistent results.
Do not perform function call in argument defaults;
instead, perform the call within the function,
or read the default from a module-level singleton variable

In all these cases, the argument is designed to be persistent.
Do not assert blind exception: `Exception`
Found useless expression. Either assign it to a variable or remove it.
Loop control variable overrides iterable it iterates
abstract base class has no abstract methods or properties
Star-arg unpacking after a keyword argument is strongly discouraged
`except` handlers should only be exception classes or tuples of exception classes
Do not use mutable data structures for `ContextVar` defaults
B909 Mutation to loop iterable during iteration
Co-authored-by: Joren Hammudoglu <jhammudoglu@gmail.com>
Co-authored-by: Joren Hammudoglu <jhammudoglu@gmail.com>
Co-authored-by: Joren Hammudoglu <jhammudoglu@gmail.com>
Co-authored-by: Joren Hammudoglu <jhammudoglu@gmail.com>
@ngoldbaum
Copy link
Member

I think you need to do a little more to remove memusage, including in the type stubs, typing tests, and the __all__ for the numpy.testing._private.utils namespace.

The memusage function is only exercised in typing tests.
@DimitriPapadopoulos
Copy link
Contributor Author

DimitriPapadopoulos commented Apr 29, 2025

I'm not sure why the compilation fails on Windows.

numpy/random/_pcg64.pyx compilation error
  [511/521] Compiling Cython source D:/a/1/s/numpy/random/_pcg64.pyx
  FAILED: numpy/random/_pcg64.cp312-win_amd64.pyd.p/numpy/random/_pcg64.pyx.c
  "cython" "-M" "--fast-fail" "-3" D:/a/1/s/numpy/random/_pcg64.pyx -o numpy/random/_pcg64.cp312-win_amd64.pyd.p/numpy/random/_pcg64.pyx.c
  [512/521] Compiling Cython source numpy/random/_generator.pyx
  [513/521] Compiling Cython source numpy/random/mtrand.pyx
  ninja: build stopped: subcommand failed.
  Activating VS 16.11.46
  INFO: automatically activated MSVC compiler environment
  INFO: autodetecting backend as ninja
  INFO: calculating backend command to run: C:\hostedtoolcache\windows\Python\3.12.10\x64\Scripts\ninja.EXE
  error: subprocess-exited-with-error
  
  Preparing metadata (pyproject.toml) did not run successfully.
  exit code: 1
  
  See above for output.
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  full command: 'C:\hostedtoolcache\windows\Python\3.12.10\x64\python.exe' 'C:\hostedtoolcache\windows\Python\3.12.10\x64\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py' prepare_metadata_for_build_wheel 'C:\Users\VSSADM~1\AppData\Local\Temp\tmpxa2humy0'
  cwd: D:\a\1\s
  Preparing metadata (pyproject.toml): finished with status 'error'
error: metadata-generation-failed

Encountered error while generating package metadata.

@ngoldbaum
Copy link
Member

That's odd, let's see if it's reproducible. I re-triggered the failed build.

@@ -36,7 +36,7 @@
'assert_equal', 'assert_almost_equal', 'assert_approx_equal',
'assert_array_equal', 'assert_array_less', 'assert_string_equal',
'assert_array_almost_equal', 'assert_raises', 'build_err_msg',
'decorate_methods', 'jiffies', 'memusage', 'print_assert_equal',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh, I'm really sorry for not realizing this until now, but it turns out that np.testing._private.utils.memusage is re-exported in the public NumPy API as np.testing.memusage:

In [1]: import numpy as np

In [2]: np.testing.memusage
Out[2]: <function numpy.testing._private.utils.memusage()>

In [3]: np.testing.memusage?
Signature: np.testing.memusage()
Docstring: Return memory usage of running python. [Not implemented]
File:      ~/.pyenv/versions/3.13.3/lib/python3.13/site-packages/numpy/testing/_private/utils.py
Type:      function

So, we definitely shouldn't delete it. Sorry for pointing you in completely the wrong direction.

Now, that said, there's all kinds of cruft in the np.testing namespace we should deprecate and clean up, but that should be in a separate PR.

Can you revert the changes that deleted memusage? Again, sorry for pointing you in the wrong direction in my prior code review. I'm happy to do this myself if you would like me to fix my own mistake :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants