8000 Document how (not) to use basestring · Issue #1537 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Document how (not) to use basestring #1537

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

Closed
JukkaL opened this issue May 16, 2016 · 3 comments
Closed

Document how (not) to use basestring #1537

JukkaL opened this issue May 16, 2016 · 3 comments

Comments

@JukkaL
Copy link
Collaborator
JukkaL commented May 16, 2016

Users have started using basestring in stubs and elsewhere. Using basestring as a function argument type in a stub seems relatively benign (though inconsistent), but using as a return type is problematic for a few reasons:

  • basestring currently has no operations and can't be passed to functions expecting unicode or even Union[bytes, unicode].
  • Using isinstance() doesn't work well, since it's not a union. It's often necessary to use something like if isinstance(x, bytes): ... elif isinstance(x, unicode) instead of if isinstance(x, bytes): ... else: ....

Not sure what we should tell users. Perhaps say that using basestring as an argument type is not recommended (at least for now), and Union[bytes, unicode] is the recommended, hopefully future-proof alternative in Python 2 -- even though it's currently equivalent to just unicode.

Also say the basestring should not be used as a return value or attribute type, but Union[bytes, unicode] should also be avoided there. In these contexts str, bytes, unicode and AnyStr are better, whenever possible.

See also python/typeshed#202.

@gvanrossum
Copy link
Member

See also python/typeshed#138

@lucaswiman
Copy link
lucaswiman commented Jan 28, 2017

This comes up in attempting to make python-3 compatible code with six, since six.string_types is aliased to basestring (both in six and in the stub). Here's a simplified example from a codebase I'm attempting use mypy on:

import six
from typing import Any

def c(a, b):  # type: (Any, Any) -> int
    if isinstance(a, six.string_types):
        return getattr(b, a)
    else:
        return b[a]

This fails with:

$ mypy --py2 getattr_six.py 
getattr_six.py: note: In function "c":
getattr_six.py:6: error: Argument 2 to "getattr" has incompatible type "basestring"; expected "unicode"

@JukkaL commented:

Having basestring aliased to Union[bytes, unicode] might work better (I think somebody suggested it), though the resulting error messages could be confusing, as they wouldn't refer to basestring.

I think updating the type signature in the stub type of six.text_type to (Union[bytes, unicode], ) wouldn't be more confusing, since six.text_type already doesn't mention basestring.

@hauntsaninja
Copy link
Collaborator

What is Python 2?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants
0