Skip to content

Char type as a special length one string type #4334

Closed

Description

@yegle

Cross post from typing repo: python/typing#510

The original issue contains some additional context, but is it feasible to implement a Char type as a special case of Text type?

Activity

  1. elazarg commented on Dec 7, 2017

    @elazarg
    Contributor

    I think this is similar (in challenges and special-casing) to having a singleton type. (Imagine unary alphabet; for most purposes mypy doesn't know it's not). But it's less general and requires understanding of len for flow-dependent checking. If we can do this, we might as well add singleton types, and a special "AnyChar" value. Then Char is Singleton[AnyChar].

  2. JukkaL commented on Dec 7, 2017

    @JukkaL
    Collaborator

    I've sometimes wondered about having a character type, but haven't yet found very convincing use cases.

    One option would be to implement this as a separate string-like type with a promotion from Char to Text (similar to the promotion from int to float that we already have). Both would be represented at runtime by the same type. A single-character string literal would have type Char (maybe only in a Char type context, though).

    However, there are probably simpler ways to catch accidentally using str when Iterable[str] or Sequence[str] is expected. For example, we could have a strictness option that would flag these. If we had such option, I'd probably turn it on in codebases that I work on, for sure.

  3. JukkaL commented on Jan 29, 2020

    @JukkaL
    Collaborator

    Literal['x'] can sometimes be used for this use case. More generally, there is no concrete proposal that seems practical, so I'm closing the issue.

  4. Hibou57 commented on Sep 3, 2020

    @Hibou57

    There is also a common misconception about what a character is. There are code points and graphemes. What one sees in a text editor, are graphemes, what a computer sees in a file, are code points. Some graphemes are made of two code points, a code point for a base glyph and a code point for an overlayed glyph.

    As far I experienced it, Python deals with code point, so a grapheme made of two glyphs, will be a string of length 2.

    The best to deal with code points individually, is to deal with their integer value which can be retrieve with the ord() function.

    I would rather say the issue is that Python does not provide a built-in iterator to iterate over a string as a sequence of code point integer values, but one can define such an iterator easily. I also believe there is no Python function to split a string into graphemes, by the way (unless i’m wrong).

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions