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
ENH: inline yt.funcs.is_sequence #3499
Conversation
b72885e
to
1b51360
Compare
@@ -594,7 +595,7 @@ def to_frb(self, width, resolution, center=None, height=None, periodic=False): | |||
) | |||
|
|||
validate_width_tuple(width) | |||
if is_sequence(resolution): | |||
if isinstance(resolution, Iterable): |
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 is one of the few cases that I corrected the logic instead of producing exactly equivalent code
@@ -253,7 +254,11 @@ def __init__(self, data_source, figure_size, fontsize): | |||
self.data_source = data_source | |||
self.ds = data_source.ds | |||
self.ts = self._initialize_dataset(self.ds) | |||
if is_sequence(figure_size): | |||
if isinstance(figure_size, Sized): | |||
if len(figure_size) != 2: |
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.
I added a sensible check here
119598e
to
fa11298
Compare
…string to discourage usage
fa11298
to
7ab33c1
Compare
7ab33c1
to
ae603e0
Compare
Can you remind me if this has or had any impact on our usage of |
@matthewturk |
ae603e0
to
5a55441
Compare
The GH workflow failures seem to be due to a change upstream on MacOs + home-brew + proj + gets + Python 3.9.7... So fixing this is out of scope here. However, the failures on Jenkins are real, so I'll make this a draft for now. |
5a55441
to
d4045ee
Compare
This allowed me to discover a bug in unyt yt-project/unyt#201 |
Turns out the bug isn't in unyt, but in numpy. I think this PR is doomed because numpy arrays have weird behaviours and don't seem to play well with See numpy/numpy#19833, and a related, long standing issue numpy/numpy#2776 I'll open another PR to try and reflect this knowledge better in our code base. |
PR Summary
This is in support to YTEP 0038, and more broadly in support to better typing in yt.
It is a follow up to #2893, in which I renamed
has_len
intois_sequence
based on a misconceptionI was having about the word "sequence" in Python.
I encourage reviewers to read this short page https://docs.python.org/3/library/collections.abc.html
So, the main reason this function needs to be discouraged is that its name is misleading. It is not equivalent to the following implementation
On the other hand, it is equivalent to
and because this implementation is try-free as well as simpler, I replaced the current implem with it.
Now, since it is apparent that it can now be inlined at the same cost (one import), it seems reasonable to drop its usage internally, but we can still keep it for now, to avoid breaking downstream applications.
In many places where it is used, checking that a given object knows the
__len__
protocol isn't exactly the appropriate thing to do. Sometimes we really want to check that objets are instances ofcollections.abc.Sequence
orcollections.abc.Collection
. However, to avoid breaking anything by changing a lot of code at once, I stopped at the inlining step, except in one or two obvious cases, which I will highlight here with comments.It is worth noting that
np.ndarray
objects are not considered ascollections.abc.Sequence
, because they don't implement the__reversed__
protocol. See https://docs.python.org/dev/glossary.html#term-sequence for more detailPR Checklist