Skip to content

Deprecate is_string_like #7835

Closed
Closed

Description

@anntzer

This issue is similar to #7795 ("is_numlike should be deprecated and replaced by the correct isinstance calls"): cbook.is_string_like returns True for 0-dimensional masked arrays of string dtype (but not regular arrays...), but testing for is_string_like is often followed by calling of string methods (.lower(), etc.) on the object, or passing the object to, say, open() -- neither of which work with masked arrays of string dtype. (check yourself with git grep -A2 'is_string_like(')

In other words the actual semantics of the function simply do not match how it is used.

Most of these calls should probably be replaced by isinstance(obj, six.string_types) (possibly isinstance(obj, (six.string_types, six.binary_type)) depending on the case), which is much more explicit.

Activity

  1. added this to the milestone on Jan 15, 2017
  2. tacaswell commented on Jan 15, 2017

    @tacaswell
    Member

    Why is 'isinstance' checking better than improving the duck typing?

  3. anntzer commented on Jan 15, 2017

    @anntzer
    ContributorAuthor

    Because I doubt you will ever be able to make it possible to pass 0d-masked-arrays to open(), and that it'll at least take a while before masked string arrays implement string methods (as methods, not as standalone functions), if they ever do (which I doubt).

  4. dopplershift commented on Jan 16, 2017

    @dopplershift
    Contributor

    Isn't the purpose of having a is_string_like function the ability to abstract away the way we're doing the check (i.e. duck-typing vs. isinstance)?

  5. anntzer commented on Jan 16, 2017

    @anntzer
    ContributorAuthor

    open doesn't care about abstraction. It exactly wants a string, or a string subclass.
    Likewise, calling string methods (via method syntax) doesn't work on numpy arrays of string dtype, so the abstraction is not very good.

  6. dopplershift commented on Jan 16, 2017

    @dopplershift
    Contributor

    Do we really have no uses of is_string_like that could actually us a duck string?

  7. anntzer commented on Jan 16, 2017

    @anntzer
    ContributorAuthor

    There may well be some -- but my guess is (and a quick grep suggests) that they are a minority, so I'd suggest looking at them and asking whether we really need to support them (note that as mentioned above, so far numpy 0d arrays of string dtype were actually rejected, only 0d masked arrays of string dtype were accepted, and no one seemed to complain.)

    I did just realize that indexing numpy string arrays returns a np.str_, which is a different type, but similarly to the is_numlike case, one can just define string_like = (str, np.str_) and then use isinstance(obj, string_like) (yes, this could even be the new implemetation of is_string_like if you want to keep it...).

  8. anntzer commented on Apr 22, 2017

    @anntzer
    ContributorAuthor

    Closed by #8011.

  9. modified the milestones: , on May 3, 2017
  10. added a commit that references this issue on Jul 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions