mdmintz / pynose

pynose fixes nose to extend unittest and make testing easier
https://pypi.org/project/pynose/
GNU Lesser General Public License v2.1
14 stars 6 forks source link

Use comprehensions and other ruff simplifications #19

Closed cclauss closed 9 months ago

cclauss commented 9 months ago

% ruff --select=B004,G010,PLR1701,PLR5501,RET502,SIM114 --fix

Found 23 errors (23 fixed, 0 remaining).

% ruff --select=C4,RET503,SIM110,SIM118,TRY400,UP028 --fix --unsafe-fixes

Found 23 errors (23 fixed, 0 remaining).

% ruff rule B004

unreliable-callable-check (B004)

Derived from the flake8-bugbear linter.

Fix is sometimes available.

What it does

Checks for uses of hasattr to test if an object is callable (e.g., hasattr(obj, "__call__")).

Why is this bad?

Using hasattr is an unreliable mechanism for testing if an object is callable. If obj implements a custom __getattr__, or if its __call__ is itself not callable, you may get misleading results.

Instead, use callable(obj) to test if obj is callable.

Example

hasattr(obj, "__call__")

Use instead:

callable(obj)

References

mdmintz commented 9 months ago

I learned a lot from going through those improvements. One of your changes seems unintended though.

You changed log.error to log.exception. Not the same. In the case of multithreading with the aforementioned code, we certainly want the log.error with the shortened output because log.exception adds exception info to the logging message, which is normally fine, but it'll make multithreaded output messy. Please undo that specific change.

Screenshot 2024-02-13 at 3 23 40 PM
mdmintz commented 9 months ago

Merged!