Skip to content

Treat ascii-only literals as str, not unicode when using unicode_literals #3619

Closed
@JukkaL

Description

@JukkaL

Many typeshed stubs only accept str when ascii-only unicode values also work at runtime. This causes a lot of problems with code using unicode_literals, since generally most literals will have unicode as their type. A quick fix would be to infer str as the type for ascii-only string literals when using unicode_literals, but only then. This would cause some unsafety, but it may be worth it for users using unicode_literals. The type safe solution is to manually check every str argument type in the entire typeshed and potentially replace it with unicode or similar, but this would be extremely painful and unlikely to happen any time soon. This would have no effect on code that doesn't use unicode_literals.

Example of how this would work:

from __future__ import unicode_literals

reveal_type('foo')   # str
reveal_type('ß')   # unicode

Some open questions:

  • Should u'foo' always have type unicode?
  • What's the inferred type of x in x = 'foo'?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions