Skip to content

Deprecate is_string_like, is_sequence_of_strings #8011

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

Merged
merged 5 commits into from
Apr 17, 2017

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Feb 3, 2017

While implementing #7996 (see also #7835), I had missed that numpy's str_ (and unicode_ on Py2) derive from the corresponding builtin type(s), so isinstance(x, six.text_type) is just as good. So I just inlined that and restored the old definition of is_string_like, immediately deprecating it.

Would also (upon removal) close #7725.

@QuLogic
Copy link
Member

QuLogic commented Feb 3, 2017

Has this been true since NumPy 1.7.1?

@anntzer
Copy link
Contributor Author

anntzer commented Feb 3, 2017

Yes.

$ conda create -n tmpenv --yes python=3.3 numpy=1.7.1 >/dev/null && source activate tmpenv &&
python -c 'from numpy import str_, unicode_; print(str in str_.__mro__, str in unicode_.__mro__)'
True True

and likewise on Py2 (but with unicode in unicode_.__mro__)

@QuLogic QuLogic added this to the 2.1 (next point release) milestone Feb 3, 2017
@QuLogic
Copy link
Member

QuLogic commented Feb 3, 2017

Looks like an example might have broken.

@anntzer
Copy link
Contributor Author

anntzer commented Feb 3, 2017

Fixed.
Axes3D.scatter now doesn't accept 2D inputs anymore (which were silently flattened before). If we want to document the change it should probably go with #7786.

anntzer added 2 commits April 15, 2017 00:11
I had missed that numpy's `str_` (and `unicode_` on Py2) derive from the
corresponding builtin type(s), so `isinstance(x, six.string_types)` is
just as good.
`Axes3D.scatter` no longer flattens a 2D color input.
@anntzer
Copy link
Contributor Author

anntzer commented Apr 15, 2017

rebased. bumping :-)

means that arrays are now never num-like or string-like regardless of their
dtype.
`cbook.is_numlike` now only checks that its argument is an instance of
``(numbers.Number, np.Number)`` and. In particular, this means that arrays are
Copy link
Member

Choose a reason for hiding this comment

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

and what?

Copy link
Contributor Author

@anntzer anntzer Apr 15, 2017

Choose a reason for hiding this comment

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

typo, fixed

c_list = []
for c in colors:
c_list.append([c]*20)
c_list.extend([c] * 20)
Copy link
Member

Choose a reason for hiding this comment

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

Is this actually related?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not really, moved out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh actually i remember now, it is.
this is because 3d scatter no longer implicitly flattens its input (see the last hunk of the diff).

Copy link
Member

Choose a reason for hiding this comment

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

Probably should add that to the API what's new, then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Also documented the similar change in #7786 at the same time.


`cbook.is_string_like` and `cbook.is_sequence_of_strings` have been
deprecated. Use ``isinstance(obj, six.string_types)`` and ``all(isinstance(o,
six.string_types) for o in obj) and not iterable(obj)`` instead.
Copy link
Member

Choose a reason for hiding this comment

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

That doesn't seem right:

In [9]: from matplotlib.cbook import iterable, is_sequence_of_strings

In [10]: obj = ['a', 'b', 'c']

In [11]: is_sequence_of_strings(obj)
Out[11]: True

In [12]: import six

In [13]: all(isinstance(o, six.string_types) for o in obj) and not iterable(obj)
Out[13]: False

(using 2.0.0, though)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the "not" needs to be removed, fixed

raise ValueError("First argument must be a sequence")
nrecs = len(args[0])
margs = []
seqlist = [False] * len(args)
for i, x in enumerate(args):
if (not is_string_like(x)) and iterable(x) and len(x) == nrecs:
if ((not isinstance(x, six.string_types)) and iterable(x)
Copy link
Member

Choose a reason for hiding this comment

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

Can drop extra parentheses.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@@ -1219,10 +1221,10 @@ def finddir(o, match, case=False):
is True require an exact case match.
"""
if case:
names = [(name, name) for name in dir(o) if is_string_like(name)]
names = [(name, name) for name in dir(o) if isinstance(name, six.string_types)]
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this too long?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@@ -789,7 +789,7 @@ def __init__(self, colors, name='from_list', N=None):
if N is None:
N = len(self.colors)
else:
if (cbook.is_string_like(self.colors) and
if (isinstance(self.colors, six.string_types) and
cbook.is_hashable(self.colors)):
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, what strings are not hashable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

probably was the case of masked string arrays. removed the now redundant check.

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 there might be a few more of these that I didn't comment on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Caught two more, should be all fixed now.

Copy link
Member

Choose a reason for hiding this comment

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

I don't see it...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

There's only one line that changes is_hashable there (this one.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah sorry, thought you meant overly long lines. Now I have removed the hashability checks too.

@@ -1289,7 +1289,7 @@ def update_from(self, other):

def _get_rgba_face(self, alt=False):
facecolor = self._get_markerfacecolor(alt=alt)
if is_string_like(facecolor) and facecolor.lower() == 'none':
if isinstance(facecolor, six.string_types) and facecolor.lower() == 'none':
Copy link
Member

Choose a reason for hiding this comment

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

Probably too long, also.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

if ((is_string_like(s1) and s1.split()[0] == "offset") or
(is_string_like(s2) and s2.split()[0] == "offset")):
if ((isinstance(s1, six.string_types) and s1.split()[0] == "offset") or
(isinstance(s2, six.string_types) and s2.split()[0] == "offset")):
Copy link
Member

Choose a reason for hiding this comment

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

Looks a bit over as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@QuLogic QuLogic changed the title Deprecate is_string_like, is_sequence_of_strings [MRG+1] Deprecate is_string_like, is_sequence_of_strings Apr 16, 2017
@@ -40,8 +39,6 @@ class BlockingInput(object):
"""
def __init__(self, fig, eventslist=()):
self.fig = fig
if not is_sequence_of_strings(eventslist):
raise ValueError("Requires a sequence of event name strings")
Copy link
Member

Choose a reason for hiding this comment

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

Why remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it's not as if the check was really useful anyways -- one can always pass a sequence of invalid event names and it'll be just as bad as of one passed in a sequence of nonstrings.

@tacaswell tacaswell merged commit 734c09f into matplotlib:master Apr 17, 2017
@anntzer anntzer deleted the is_string_like branch April 17, 2017 01:52
@QuLogic QuLogic changed the title [MRG+1] Deprecate is_string_like, is_sequence_of_strings Deprecate is_string_like, is_sequence_of_strings Apr 17, 2017
jarrodmillman added a commit to jarrodmillman/networkx that referenced this pull request Sep 10, 2017
jarrodmillman added a commit to jarrodmillman/networkx that referenced this pull request Sep 10, 2017
jarrodmillman added a commit to networkx/networkx that referenced this pull request Sep 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

is_string_like returns True for numpy object arrays
3 participants