Skip to content

Conversation

@DimitriPapadopoulos
Copy link
Contributor

In preparation for #24994.

@DimitriPapadopoulos DimitriPapadopoulos changed the title MAINT: Apply ruff/pycodestyle error rules (E) MAINT: Apply pycodestyle error rules (E) Aug 29, 2024
@charris
Copy link
Member

charris commented Aug 30, 2024

Needs rebase.

@charris
Copy link
Member

charris commented Aug 30, 2024

Might want tag ignore some of the long lines.

@charris
Copy link
Member

charris commented Sep 4, 2024

@DimitriPapadopoulos Are you intentionally keeping this a draft?

@DimitriPapadopoulos
Copy link
Contributor Author

I need to apply more E pycodestyle rules and thought you would prefer a single PR. Actually I prefer smaller changes, so I will remove the Draft label soon and leave other changes for later.

@DimitriPapadopoulos DimitriPapadopoulos marked this pull request as ready for review September 5, 2024 07:37
@DimitriPapadopoulos DimitriPapadopoulos changed the title MAINT: Apply pycodestyle error rules (E) MAINT: Start applying pycodestyle error rules (E) Sep 5, 2024
('user/index', 'numpy-user.tex', 'NumPy User Guide',
_stdauthor, 'manual'),
('reference/index', 'numpy-ref.tex', 'NumPy Reference',
_stdauthor, 'manual'),
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the continuation have an extra space?

return "'%sU%d'" % (byteorder, dtype.itemsize / 4)

elif dtype.type == str:
elif dtype.type is str:
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, this might change behavior. @seberg Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

If this was dtype == then yes, as is. This is fine, although it is a bit funny to not use it above as well.

# name: [string of chars for which it is defined,
# string of characters using func interface,
# tuple of strings giving funcs for data,
# (in, out), or (instr, outstr) giving the signature as character codes,
Copy link
Member

Choose a reason for hiding this comment

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

Might add an extra space here even if a comment.

cast._resolve_descriptors((from_dt, to_dt)))
assert(type(from_res) == from_Dt)
assert(type(to_res) == to_Dt)
assert type(from_res) is from_Dt
Copy link
Member

Choose a reason for hiding this comment

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

Might want to check that this is correct.

np.datetime64('NaT', '[D]'),
np.timedelta64(3, '[D]'),
np.timedelta64(11, '[h]'),
np.timedelta64(3*24 - 11, '[h]'))]:
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I think here and below the contents are over indented. Might be a better way to write these, maybe put the opening [ on the preceding line and dispense with the line continuation?

kwargs = {'fill_value': ''} if likefunc == np.full_like else {}
result = likefunc(b, dtype=dtype, **kwargs)
if dtype == str:
if dtype is str:
Copy link
Member

Choose a reason for hiding this comment

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

Might be worth a check.

[one],
[three],
[four]
]),
Copy link
Member

Choose a reason for hiding this comment

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

This nesting here is difficult to understand, could use a reformatting.

def test_tobits(self):
data2bits = lambda data: sum([int(x != 0) << i for i, x in enumerate(data, 0)])
def data2bits(data):
return sum([int(x != 0) << i for i, x in enumerate(data, 0)])
Copy link
Member

Choose a reason for hiding this comment

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

I'm a bit surprised a blank line wasn't added here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are automated fixes by ruff, and pycodestyle doesn't have anything to say about it at this point. I was thinking of handling this error in an upcoming PR that would handle the relevant E pycodestyle rule.


data_sqrt = self.load([math.sqrt(x) for x in data]) # load to truncate precision
data_sqrt = self.load([math.sqrt(x) # load to truncate precision
for x in data])
Copy link
Member

@charris charris Sep 5, 2024

Choose a reason for hiding this comment

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

Would be nicer to move the trailing comment to the preceding line.

class _SupportsArray(Protocol[_DType_co]):
def __array__(self) -> ndarray[Any, _DType_co]: ...
def __array__(self) -> ndarray[Any, _DType_co]:
...
Copy link
Member

Choose a reason for hiding this comment

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

I think all these typing fixes should be avoided. @jorenham Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What can I say? I'm just enforcing pycodestyle rules, not my choice. Unlike ruff, pycodestyle appears to be very opinionated, in the sense that errors cannot be silenced with noqa.

Copy link
Member

Choose a reason for hiding this comment

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

# fmt: skip maybe?

Copy link
Member

Choose a reason for hiding this comment

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


substr = list_re.sub(listrepl, substr) # convert all lists to named templates
substr = list_re.sub(listrepl, substr) # convert all lists to named templates
# newnames are constructed as needed
Copy link
Member

Choose a reason for hiding this comment

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

Might want to line this up. Maybe better, put the comments on the preceding line. I hate trailing comments :)

return writestr


include_src_re = re.compile(r"(\n|\A)\s*include\s*['\"](?P<name>[\w\d./\\]+\.src)['\"]", re.I)
Copy link
Member

Choose a reason for hiding this comment

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

This line seems long ...

for mat in self.rshft_all:
tz(mat.astype(dt))
if dt != object:
if dt != object: # noqa: E721
Copy link
Member

Choose a reason for hiding this comment

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

I'm curious why dt is not object didn't work here.

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 am considering moving this commit to a PR of its own, for a separate discussion.

numpy/ma/core.py Outdated
mask = (
f(getmaskarray(a), np.ones(np.shape(v), dtype=bool), mode=mode)
| f(np.ones(np.shape(a), dtype=bool), getmaskarray(v), mode=mode)
f(getmaskarray(a), np.ones(np.shape(v), dtype=bool), mode=mode) |
Copy link
Member

Choose a reason for hiding this comment

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

IIRC, Python changed the style to prefer putting the operator in front. Don't recall if that was for Python or C code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, see W503 / W504.

To be honest, I don't care about formatting, just about silencing pycodestyle. I'd rather let a formatter do the formatting for me. However, I understand black or ruff don't work well on numerical code.

Copy link
Member

Choose a reason for hiding this comment

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

I'm happy with leaving the change here, just reminded of the preference change.

class SubClass(npt.NDArray[np.float64]): ...
class SubClass(npt.NDArray[np.float64]):
...

Copy link
Member

Choose a reason for hiding this comment

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

Again the typing.

@charris
Copy link
Member

charris commented Sep 5, 2024

That was no fun, it would be better to keep the PR size down, and maybe do fewer style changes together. Going through a bunch of lines that add spaces in comments or blank lines is pretty easy, but mixing them in with more complicated fixes slows things down.

@jorenham
Copy link
Member

jorenham commented Sep 5, 2024

For the static typing-related changes I agree with @charris that current "inlined ellipsis style", i.e. : ..., is conventional.
I believe that ruff format always enforces this for (at least) typing.Protocol, and always does so in .pyi stubs.

@charris
Copy link
Member

charris commented Sep 11, 2024

@DimitriPapadopoulos What are your plans here? With a few cleanups we could put this in.

@charris
Copy link
Member

charris commented Sep 25, 2024

@DimitriPapadopoulos We will be branching 2.2 in six weeks or so. It would be good to get these cleanups in before that happens.

@DimitriPapadopoulos
Copy link
Contributor Author

@charris I am afraid I unintentionally deleted this branch 😳 That's not necessarily a big deal because I am not certain any more that fixing Pycodestyle issues in the whole codebase is the best strategy to move to the ruff linter. Perhaps we can continue the discussion about the best strategy in #24994.

@DimitriPapadopoulos
Copy link
Contributor Author

Superseded by #27300.

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