Skip to content

Commit 09c2186

Browse files
authored
Merge pull request numpy#27289 from DimitriPapadopoulos/RUF
MAINT: Start applying ruff rules (RUF)
2 parents 6a6a08e + 3864a97 commit 09c2186

File tree

13 files changed

+23
-21
lines changed

13 files changed

+23
-21
lines changed

benchmarks/benchmarks/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def dirty_lock(lock_name, lock_on_count=1):
4242
count = 0
4343
f.seek(0)
4444
f.truncate()
45-
f.write(f"{str(count)} {str(ppid)}")
45+
f.write(f"{count} {ppid}")
4646
except OSError:
4747
pass
4848
return False

numpy/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,9 @@ def _mac_os_check():
477477
for _wn in w:
478478
if _wn.category is exceptions.RankWarning:
479479
# Ignore other warnings, they may not be relevant (see gh-25433).
480-
error_message = f"{_wn.category.__name__}: {str(_wn.message)}"
480+
error_message = (
481+
f"{_wn.category.__name__}: {_wn.message}"
482+
)
481483
msg = (
482484
"Polyfit sanity test emitted a warning, most likely due "
483485
"to using a buggy Accelerate backend."

numpy/__init__.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3387,13 +3387,13 @@ class ufunc:
33873387
# raise a ValueError ufuncs with that don't accept two input
33883388
# arguments and return one output argument. Because of that we
33893389
# can't type them very precisely.
3390-
def reduce(self, /, *args: Any, **kwargs: Any) -> NoReturn | Any: ...
3391-
def accumulate(self, /, *args: Any, **kwargs: Any) -> NoReturn | NDArray[Any]: ...
3392-
def reduceat(self, /, *args: Any, **kwargs: Any) -> NoReturn | NDArray[Any]: ...
3393-
def outer(self, *args: Any, **kwargs: Any) -> NoReturn | Any: ...
3390+
def reduce(self, /, *args: Any, **kwargs: Any) -> Any: ...
3391+
def accumulate(self, /, *args: Any, **kwargs: Any) -> NDArray[Any]: ...
3392+
def reduceat(self, /, *args: Any, **kwargs: Any) -> NDArray[Any]: ...
3393+
def outer(self, *args: Any, **kwargs: Any) -> Any: ...
33943394
# Similarly at won't be defined for ufuncs that return multiple
33953395
# outputs, so we can't type it very precisely.
3396-
def at(self, /, *args: Any, **kwargs: Any) -> NoReturn | None: ...
3396+
def at(self, /, *args: Any, **kwargs: Any) -> None: ...
33973397

33983398
# Parameters: `__name__`, `ntypes` and `identity`
33993399
absolute: _UFunc_Nin1_Nout1[L['absolute'], L[20], None]

numpy/_core/numerictypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def isdtype(dtype, kind):
452452
elif isinstance(kind, str):
453453
raise ValueError(
454454
"kind argument is a string, but"
455-
f" {repr(kind)} is not a known kind name."
455+
f" {kind!r} is not a known kind name."
456456
)
457457
else:
458458
try:

numpy/_core/tests/test_stringdtype.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ def test_dtype_repr(dtype):
116116
if not hasattr(dtype, "na_object") and dtype.coerce:
117117
assert repr(dtype) == "StringDType()"
118118
elif dtype.coerce:
119-
assert repr(dtype) == f"StringDType(na_object={repr(dtype.na_object)})"
119+
assert repr(dtype) == f"StringDType(na_object={dtype.na_object!r})"
120120
elif not hasattr(dtype, "na_object"):
121121
assert repr(dtype) == "StringDType(coerce=False)"
122122
else:
123123
assert (
124124
repr(dtype)
125-
== f"StringDType(na_object={repr(dtype.na_object)}, coerce=False)"
125+
== f"StringDType(na_object={dtype.na_object!r}, coerce=False)"
126126
)
127127

128128

numpy/core/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import warnings
22

33

4-
def _raise_warning(attr: str, submodule: str = None) -> None:
4+
def _raise_warning(attr: str, submodule: str | None = None) -> None:
55
new_module = "numpy._core"
66
old_module = "numpy.core"
77
if submodule is not None:

numpy/ctypeslib.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,11 @@ def ndpointer(dtype=None, ndim=None, shape=None, flags=None):
282282
283283
"""
284284

285-
# normalize dtype to an Optional[dtype]
285+
# normalize dtype to dtype | None
286286
if dtype is not None:
287287
dtype = _dtype(dtype)
288288

289-
# normalize flags to an Optional[int]
289+
# normalize flags to int | None
290290
num = None
291291
if flags is not None:
292292
if isinstance(flags, str):
@@ -304,7 +304,7 @@ def ndpointer(dtype=None, ndim=None, shape=None, flags=None):
304304
raise TypeError("invalid flags specification") from e
305305
num = _num_fromflags(flags)
306306

307-
# normalize shape to an Optional[tuple]
307+
# normalize shape to tuple | None
308308
if shape is not None:
309309
try:
310310
shape = tuple(shape)

numpy/f2py/_backends/_distutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def compile(self):
4242
i = get_info(n)
4343
if not i:
4444
print(
45-
f"No {repr(n)} resources found"
45+
f"No {n!r} resources found"
4646
"in system (try `f2py --help-link`)"
4747
)
4848
dict_append(ext_args, **i)

numpy/f2py/capi_maps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ def routsign2map(rout):
627627
ln = k
628628
break
629629
lcb_map[ln] = un[1]
630-
elif 'externals' in rout and rout['externals']:
630+
elif rout.get('externals'):
631631
errmess('routsign2map: Confused: function %s has externals %s but no "use" statement.\n' % (
632632
ret['name'], repr(rout['externals'])))
633633
ret['callprotoargument'] = getcallprotoargument(rout, lcb_map) or ''

numpy/f2py/crackfortran.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,7 +2079,7 @@ def postcrack(block, args=None, tab=''):
20792079
block = analyzecommon(block)
20802080
block['vars'] = analyzevars(block)
20812081
block['sortvars'] = sortvarnames(block['vars'])
2082-
if 'args' in block and block['args']:
2082+
if block.get('args'):
20832083
args = block['args']
20842084
block['body'] = analyzebody(block, args, tab=tab)
20852085

@@ -2095,7 +2095,7 @@ def postcrack(block, args=None, tab=''):
20952095
if 'name' in block:
20962096
name = block['name']
20972097
# and not userisdefined: # Build a __user__ module
2098-
if 'externals' in block and block['externals']:
2098+
if block.get('externals'):
20992099
interfaced = []
21002100
if 'interfaced' in block:
21012101
interfaced = block['interfaced']

0 commit comments

Comments
 (0)