python / python/typing

Idea: add Char type to represent Text with size = 1

Open
#510 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: feature
Dominant language
Python
Stars
1.8k
Forks
302
Avg merge
23h
Merged PRs (30d)
8

Description

Due to the lack of character type in Python, a function that accept List[Text] will need to be extra careful when generalize the accepted argument type. Iterable[Text] and Sequence[Text] clearly doesn't work:

In [1]: import collections.abc

In [2]: isinstance("foo", collections.abc.Sequence)
Out[2]: True

In [3]: isinstance("foo", collections.abc.Iterable)
Out[3]: True

The closest thing you can do is to do something like Union[List[Text], Set[Text], Tuple[Text]] but it will not work with things like KeysView.

How about having a typing.Char to represent Text with length=1, and dedicate Text to a type with at least 2 characters?

Some examples to illustrate the usage:

def foo(xs: Iterable[Text]):
  return '|'.join(xs)

# list(Text) should return List[Char]
foo("abcd") # error
foo(list("abcd")) # correct, List[Char] is a special case of List[Text]

def bar(xs: Iterable[Char]):
  return '|'.join(xs)

bar("abcd") # correct, Text is a Iterable[Char]
bar(list("abcd")) # correct, List[Char] is a Iterable[Char]
bar(["ab", "cd"]) # error, List[Text] is not a Iterable[Char]

a: Char = 'a'

a[1] # error, Char cannot be indexed beyond index 0
a[0] # should be an error from type checking's PoV, Char should never be indexed
a[:] # should be an error from type checking's PoV, Char should never be copied as a slice

b: Text = 'b'
a + b # should be an error from type checking's PoV, Char and Text should be considered incompatible types
typing.cast(Text, a) + b # correct

c = a + a # correct, concatenate Char gets Text (but I'm not sure if this is reasonable though, what if we do (a + a) + a ?)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files, tests, or entry points; begin by reviewing the proposed Char and Text semantics in the issue. Done means an agreed design covering iterable behavior, indexing and slicing, compatibility, and concatenation rules.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.