Defining a generic class, are there any cases which the type argument cannot be inferred?

Defining a generic class, the doc below says the type argument can “usually” be inferred, which means not “always” so sometimes the type argument won’t be inferred:

When creating instances of generic classes, the type argument can usually be inferred.

class Box(Generic[T]):
    def __init__(self, content: T) -> None:
        self.content = content

Box(1)       # OK, inferred type is Box[int]
Box[int](1)  # Also OK
Box[int]('some string')  # error: Argument 1 to "Box" has incompatible type "str"; expected "int"

So now, are there any cases which the type argument cannot be inferred?

You can easily see that happening with code that uses non-typehinted imports, or ones from C.

from my_c_module_that_checkers_cant_find import attr

Box(attr) # Box[Undefined]

These cases (there are other ways to get this too) are quite rare for normal hinted code, so it is unlikely you’ll encounter it a lot.

Is the point of the docs, not that the type argument can always be inferred as object, but that if you write dynamic code such as below, then it’s not straightforward to infer T as a non-trivial type that’s useful to the user of the type system?

x=1
x="some string"
Box(x)

Any generic type without constructor fits the bill:

class Box[T]:
    content: T

box = Box()  # What's in the box??

There’s nothing wrong with this definition, yet T@Box cannot be inferred.

But in practice these usually come with some alternative way of constructing them, that does allow type-checkers to infer the generic type, for instance:

def get_box[T](content: T, /) -> Box[T]:
    box = Box()
    box.content = content
    return box

box = get_box(7)  # Box[int]