Skip to content

mypy error with ViewList in sphinx extensions #2139

@samtygier-stfc

Description

@samtygier-stfc

Summary

A recent update to types-docutils (from 0.20.0.20240311 to 0.20.0.20240314) has added typing to ViewList. ( python/typeshed@a1bfd65 python/typeshed#11469 ). We also recently turned on mypy typing for 3rd party types, so are getting the following errors

docs/ext/release_notes.py:30: error: Need type annotation for "rst"  [var-annotated]
docs/ext/release_notes.py:56: error: Argument 2 to "nested_parse_with_titles" has incompatible type "ViewList[Any]"; expected "StringList"  [arg-type]
docs/ext/operations_user_doc.py:84: error: Need type annotation for "rst"  [var-annotated]
docs/ext/operations_user_doc.py:91: error: Argument 2 to "nested_parse_with_titles" has incompatible type "ViewList[Any]"; expected "StringList"  [arg-type]

This is because ViewList is a generic container.
https://github.com/python/typeshed/blob/a1bfd65e9fa92e1bb61a475c7622ba0376d936d5/stubs/docutils/docutils/statemachine.pyi#L114

mypy can only infer the type if the generic type is used in the constructor. This is unlike built in containers, which can infer from the constructor or a method call

l1 = [1]
reveal_type(l1) # -> Revealed type is "builtins.list[builtins.int]

l2 = []
l2.append(1)
reveal_type(l2) # -> Revealed type is "builtins.list[builtins.int]"

For custom classes this does not work python/mypy#13134

T = TypeVar("T")
class MyContainer(Generic[T]):
	def set(self, x: T):
		self.item = x

mc = MyContainer()
mc.set(1)
reveal_type(mc) # -> Revealed type is "custom_class.MyContainer[Any]

Steps To Reproduce

Expected Behaviour

Current Behaviour

Context

Failure Logs

Screenshot(s)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions