Skip to content

conao3-playground/python-newtype-upcast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

python-newtype-upcast

Sequence rather than list needs to be used to recognise implicit upcasts.

from typing import NewType
from collections.abc import Sequence


TName = NewType("TName", str)


def char_count(name: str) -> int:
    return len(name)


def count1(names: list[str]) -> int:
    return len(names)


def count2(names: Sequence[str]) -> int:
    return len(names)


def main() -> None:
    names = [TName("John"), TName("Jane"), TName("Joe")]

    # linter happy
    print(char_count(names[0]))

    # Argument of type "list[TName]" cannot be assigned to parameter "names" of type "list[str]" in function "count1"
    # "list[TName]" is incompatible with "list[str]"
    #     Type parameter "_T@list" is invariant, but "TName" is not the same as "str"
    #     Consider switching from "list" to "Sequence" which is covariantPylancereportGeneralTypeIssues
    print(count1(names))

    # linter happy
    print(count2(names))

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages