Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mypy error with ViewList in sphinx extensions #2139

Closed
samtygier-stfc opened this issue Mar 14, 2024 · 0 comments · Fixed by #2141
Closed

mypy error with ViewList in sphinx extensions #2139

samtygier-stfc opened this issue Mar 14, 2024 · 0 comments · Fixed by #2141

Comments

@samtygier-stfc
Copy link
Collaborator

samtygier-stfc commented Mar 14, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant