While trying to pass the conformance tests for Zuban, I have encountered a few fundamental issues, this is #3 of 4.
Zuban (like Mypy) infers Never for unspecified type parameters (see example below). This happens quite a lot. For example list() is inferred as list[Never] initially. Both type checkers add an error in foo = list() if there is no foo.append(...) that completes this partial type.
This behavior is however against the spec. I feel like this is unfortunate, because avoiding Any is more strict behavior.
This is the relevant part of the spec:
Otherwise, the default value for the type parameter (or Any, if no default is provided) is assumed. Example:
class Node[T]:
x: T # Instance attribute (see below)
def __init__(self, label: T | None = None) -> None:
...
x = Node('') # Inferred type is Node[str]
y = Node(0) # Inferred type is Node[int]
z = Node() # Inferred type is Node[Any]
There are two different ways we could handle this:
(1) Allow both ways
This would mean a small change to the spec: I’m not sure about a good wording, but I would probably say or Any/Never, if no default is assumed and mention in the comment below Node[Any]/Node[Never].
(2) Mandate Never/An error when assigning to a variable
This would avoid the case that z = Node() is inferred without an error. For Pyright this is as easy as enabling reportUnknownVariableType=true and Mypy/Zuban already do this.
z = Node() # Pyright: Type of "z" is partially unknown
I’m not sure though how strict the wording should be here. I personally care most about the assignment case. I feel like this is the most typical case where people will start modifying attributes and therefore unsafety unfolds. Pyright infers Node[Unknown] even with reportUnknownVariableType=true on. I feel like that’s a fine choice as well, since the error is generated on assignment.
My opinion
I’m personally in favor of (2). I believe we should enforce strict behavior in the conformance tests as much as possible even if the defaults work in a non-strict way. Pyright does a lot of things to make the type system easier for its users, but this should not stop us from specifying stronger guarantees.
I will add a PR to the typing repository and change the conformance tests/spec, for wherever the consensus lands. I hope somebody will help me with good wording.