Mypy: Unable to specify a default value for a generic parameter

Created on 19 Jul 2017  路  14Comments  路  Source: python/mypy

Simplified Example:

from typing import TypeVar

_T = TypeVar('_T')
def foo(a: _T = 42) -> _T:  # E: Incompatible types in assignment (expression has type "int", variable has type "_T")
    return a

Real World example is something closer to:

_T = TypeVar('_T')
def noop_parser(x: str) -> str:
    return x

def foo(value: str, parser: Callable[[str], _T] = noop_parser) -> _T:
    return parser(value)
feature priority-0-high topic-type-variables

Most helpful comment

I think the problem here is that in general, e.g. if there are other parameters also using _T in their type, the default value won't work.

Wait. Why is that a problem? If default value doesn鈥檛 work for some calls, that should be a type error at the call site.

_T = TypeVar('_T')
def foo(a: _T = 42, b: _T = 42) -> List[_T]:
    return [a, b]

foo()  # This should work.
foo(a=17)  # This should work.
foo(b=17)  # This should work.
foo(a="foo")  # This should be a type error (_T cannot be both str and int).
foo(b="foo")  # This should be a type error (_T cannot be both int and str).
foo(a="foo", b="foo")  # This should work.

This default is, strictly speaking, not safe. This would require a lower bound for _T1 in order to be safe.

Only if we retain the requirement that the default value always works, which is not what鈥檚 being requested here. The desired result is:

_T1 = TypeVar('_T1', bound=Mapping)
class RawConfigParser(_parser, Generic[_T1]):
    def __init__(self, dict_type: Type[_T1] = OrderedDict) -> None: ...

    def defaults(self) -> _T1: ...

class UserMapping(Mapping):
    ...

RawConfigParser[OrderedDict]()  # This should work.
RawConfigParser[UserMapping]()  # This should be a type error (_T1 cannot be both UserMapping and OrderedDict).
RawConfigParser[UserMapping](UserMapping)  # This should work.

All 14 comments

I think the problem here is that in general, e.g. if there are other parameters also using _T in their type, the default value won't work. Since this is the only parameter, you could say that this is overly restrictive, and if there are no parameters, the return type should just be determined by that default value (in your simplified example, you'd want it to return int). But I'm not so keen on doing that, since it breaks as soon as another generic parameter is used.

To make this work, you can use @overload, e.g.

@overload
def foo() -> int: ...
@overload
def foo(a: _T) -> _T: ...
def foo(a = 42):  # Implementation, assuming this isn't a stub
    return a

That's what we do in a few places in typeshed too (without the implementation though), e.g. check out Mapping.get() in typing.pyi.

I'm running in to a similar issue when trying to iron out a bug in the configparser stubs. Reducing RawConfigParser to the relevant parts gives:

_section = Mapping[str, str]

class RawConfigParser:
    def __init__(self, dict_type: Mapping[str, str] = ...) -> None: ...

    def defaults(self) -> _section: ...

This is incorrect at the moment, because dict_type should (as the name indicates) be a type. Furthermore, defaults() returns an instance of dict_type, so this seems like a good application of generics. The following works as expected when a dict_type is passed (i.e. reveal_type(RawConfigParser(dict_type=dict).default()) gives builtins.dict*[Any, Any]):

_T1 = TypeVar('_T1', bound=Mapping)

class RawConfigParser(Generic[_T1]):
    def __init__(self, dict_type: Type[_T1] = ...) -> None: ...

    def defaults(self) -> _T1: ...

but when you don't pass a dict_type, mypy can't infer what type it should be using and asks for a type annotation where it is being used. This is less than ideal for the default instantiation of the class (which will be what the overwhelming majority of people are using). So I tried adding the appropriate default to the definition:

_T1 = TypeVar('_T1', bound=Mapping)

class RawConfigParser(_parser, Generic[_T1]):
    def __init__(self, dict_type: Type[_T1] = OrderedDict) -> None: ...

    def defaults(self) -> _T1: ...

but then I hit the error that OP was seeing:

stdlib/3/configparser.pyi:51: error: Incompatible default for argument "dict_type" (default has type Type[OrderedDict[Any, Any]], argument has type Type[_T1])

This feels like something that isn't currently supported, but I'm not sure if I'm missing a way that I could do this...

@OddBloke
This default is, strictly speaking, not safe. This would require a _lower_ bound for _T1 in order to be safe.

@ilevkivskyi I don't follow, I'm afraid; could you expand on that a little, please?

Consider this code:

class UserMapping(Mapping):
    ...
RawConfigParser[UserMapping]()

In this case OrderedDict will be "assigned" to dict_type, which will be Type[UserMapping], which is not a supertype of Type[OrderedDict]. In order for the default value to be safe it is necessary to have some guarantee that _T1 will be always a _supertype_ of OrderedDict (this is called a lower bound), but this is not supported, only upper bounds are allowed for type variables.

@ilevkivskyi Thanks, that makes sense. Do you think what I'm trying to do is unrepresentable as things stand?

I didn't think enough about this, but my general rule is not to be "obsessed" with precise types, sometimes Any is OK. If a certain problem appears repeatedly, then maybe we need to improve something (also now we have plugin system). In this particular case my naive guess would be to play with overloads, as Guido suggested (note that __init__ can also be overloaded).

Allowing default values for generic parameters might also enable using type constraints to ensure that generics are only used in specified ways. This is a bit esoteric, but here is an example of how you might use the same implementation for a dict and a set while requiring that consumers of the dict type always provide a value to insert() while consumers of the set type never do.

class Never(Enum):
    """An uninhabited type"""

class Only(Enum):
    """A type with one member (kind of like NoneType)"""
    ONE = 1

NOT_PASSED = Only(1)

class MyGenericMap(Generic[K, V]):

    def insert(key, value=cast(Never, NOT_PASSED)):
        # type: (K, V) -> None
        ...
       if value is NOT_PASSED:
           ....

class MySet(MyGenericMap[K, Never]):
    pass

class MyDict(MyGenericMap[K, V]):
    pass

my_set = MySet[str]()
my_set.insert('hi')           # this is fine, default value matches concrete type
my_set.insert('hi', 'hello')  # type error, because 'hello' is not type never

my_dict = MyDict[str, int]()
my_dict.insert('hi')          # type error because value is type str, not Never
my_dict.insert('hi', 22)      # this is fine, because the default value is not used

Huh, this just came up in our code.

This request already appeared five times, so I am raising priority to high.

I think my preferred solution for this would be to support lower bounds for type variables. It is not too hard to implement (but still a large addition), and at the same time it may provide more expressiveness in other situations.

A second part for the workaround with overload should be noted: The header of implementation of the overloaded function must be without a generic annotation for that variable, but it is not acceptable for more complex functions where the body should be checked. Only the binding between input and output types is checked by overloading. The most precise solution for the body seems to encapsulate it by a new function with the same types, but without a default.

@overload
def foo() -> int: ...
@overload
def foo(a: _T) -> _T: ...

def foo(a = 42):  # unchecked implementation
    return foo_internal(a)

def foo_internal(a: _T) -> _T:
    # a checked complicated body moved here
    return a

Almost everything could be checked even in more complicated cases, but the number of necessary overloaded declarations could rise exponentially by 2 ** number_of generic_vars_with_defaults.

EDIT

Another solution is to use a broad static type for that parameter and immediately assign it in the body to the original exact generic type. It is a preferable solution for a class overload.
An example is a database cursor with a generic row type:

_T = TypeVar('_T', list, dict)

class Cursor(Generic[_T]):
    @overload
    def __init__(self, connection: Connection) -> None: ...
    @overload
    def __init__(self, connection: Connection, row_type: Type[_T]) -> None: ...
    def __init__(self, connection: Connection, row_type: Type=list) -> None:
        self.row_type: Type[_T] = row_type  # this annotation is important
        ...
    def fetchone(self) -> Optional[_T]: ...
    def fetchall(self) -> List[_T]: ...   # more methods depend on _T type

cursor = Cursor(connection, dict)  # cursor.execute(...)
reveal_type(cursor.fetchone())     # dict

I think the problem here is that in general, e.g. if there are other parameters also using _T in their type, the default value won't work.

Wait. Why is that a problem? If default value doesn鈥檛 work for some calls, that should be a type error at the call site.

_T = TypeVar('_T')
def foo(a: _T = 42, b: _T = 42) -> List[_T]:
    return [a, b]

foo()  # This should work.
foo(a=17)  # This should work.
foo(b=17)  # This should work.
foo(a="foo")  # This should be a type error (_T cannot be both str and int).
foo(b="foo")  # This should be a type error (_T cannot be both int and str).
foo(a="foo", b="foo")  # This should work.

This default is, strictly speaking, not safe. This would require a lower bound for _T1 in order to be safe.

Only if we retain the requirement that the default value always works, which is not what鈥檚 being requested here. The desired result is:

_T1 = TypeVar('_T1', bound=Mapping)
class RawConfigParser(_parser, Generic[_T1]):
    def __init__(self, dict_type: Type[_T1] = OrderedDict) -> None: ...

    def defaults(self) -> _T1: ...

class UserMapping(Mapping):
    ...

RawConfigParser[OrderedDict]()  # This should work.
RawConfigParser[UserMapping]()  # This should be a type error (_T1 cannot be both UserMapping and OrderedDict).
RawConfigParser[UserMapping](UserMapping)  # This should work.

I ran into this trying to write

_T = TypeVar('_T')
def str2int(s:str, default:_T = None) -> Union[int, _T]: # error: Incompatible default for argument "default" (default has type "None", argument has type "_T")
    try:
        return int(s)
    except ValueError:
        return default

(after figuring out that I need --no-implicit-optional mypy parameter as well). @overload is not ideal because then the body of the function is no longer type-checked.

@overload
def str2int(s:str) -> Optional[int]: ...
@overload
def str2int(s:str, default:_T) -> Union[int, _T]: ... 

def str2int(s, default = None): 
    the_body_is_no_longer_type_checked

I think this raises the bar for type-checking newbies.

Was this page helpful?
0 / 5 - 0 ratings