Skip to content

typing.NamedTuple should accept unicode names #2761

Closed

Description

@panhania

Currently, NamedTuple has the following signature associated with its constructor:

def __init__(self, typename: str, fields: Iterable[Tuple[str, Any]] = ..., *,
             verbose: bool = ..., rename: bool = ..., **kwargs: Any) -> None: ...

This causes type checking to fail in Python 2 for modules where unicode_literals is enabled since this function is usually called this way:

Foo = NamedTuple("Foo", (("bar", Bar), ("baz", Baz)))

Instead, typename should have type AnyStr and fields should have type Iterable[Tuple[AnyStr, Any]].

Activity

  1. changed the title [-]`typing.NamedTuple` should accept `AnyStr` names[/-] [+]`typing.NamedTuple` should accept `unicode` names[/+] on Jan 23, 2019
  2. JelleZijlstra commented on Jan 23, 2019

    @JelleZijlstra
    Member

    Thanks for the report and fix!

    Just to be clear, there is no such thing as an AnyStr value, since AnyStr is a type variable.

  3. panhania commented on Jan 23, 2019

    @panhania
    Author

    Just to be clear, there is no such thing as an AnyStr value, since AnyStr is a type variable.

    Ah, good to know. I was sure that AnyStr is just an alias for Union[str, unicode] but apparently I was wrong.

    I think in this particular case AnyStr should work just fine for most real world scenarios: NamedTuple is used in a "static" way with string literals and people use either bytestring literals or unicode literals without mixing the two. Nevertheless, Union[str, unicode] is obviously more general so I have fixed the code.

  4. added a commit that references this issue on Feb 9, 2019
    c8c6271
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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions