Skip to content

DefaultDict Typing is Overly Strict #7159

Closed
@blink1073

Description

@blink1073

The docs for defaultdict say:

The first argument provides the initial value for the default_factory attribute; it defaults to None. All remaining arguments are treated the same as if they were passed to the dict constructor, including keyword arguments.

However, the second argument is currently treated as a generic type that influences the type of the first argument.

The following should be valid:

>>> dict([('foo', 'bar')])
{'foo': 'bar'}
>>> dct = collections.defaultdict(dict, [('foo', 'bar')])
>>> dct
defaultdict(<class 'dict'>, {'foo': 'bar'})
>>> dct['g']
{}
>>> dct
defaultdict(<class 'dict'>, {'foo': 'bar', 'g': {}})

However, we get the error error: Argument 1 to "defaultdict" has incompatible type "Type[Dict[Any, Any]]"; expected "Optional[Callable[[], str]]".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions