Repository navigation
Function argument rejected as a base class #5865
Description
Activity
- addedbugmypy got something wrongmypy got something wrongfalse-positivemypy gave an error on correct codemypy gave an error on correct code
on Nov 2, 2018 I am having this issue as well. I am not sure why an object of type
Anyshould automatically be accepted as a base class. For example, callingf(3)satisfiesf's type definition, but3would not make for a valid base class. So, I would say that bothfandgshould error (maybe not for the reason thatferrors today).In my example, I use
Type[T]:from typing import Type, TypeVar T = TypeVar("T") def h(tp: Type[T]) -> Type[T]: class Foo(tp): pass return Foo
I would expect
hto be accepted here becausetpis declared as a type.@Victor-Savu
Anyshould be accepted as a base class because that's the point ofAny; it's an object on which any operation is valid. It's essentially a way to tell the type checker to trust the programmer. Of coursef(3)will fail at runtime and the type checker won't catch it, but that's the cost of usingAny.Your example also seems like a bug to me, but it's not quite the same as this one. Feel free to report a new issue.
Hi @JelleZijlstra! Thank you for the explanation! I went back to the documentation and I am convinced you and @JukkaL are right that the code in the first comment should produce not errors in mypy. I found this stack overflow response very helpful as well.
I would still like to make the point that not any
objectthat is taken in as an argument should be accepted by mypy:import pytest def subobject(an_object: object) -> type: class A(an_object): pass # error: Invalid type "an_object", Invalid base class return A def test_subobject() -> None: assert isinstance(Exception, type) assert isinstance(subobject(Exception), type) assert issubclass(subobject(Exception), Exception) assert isinstance(1, object) with pytest.raises(TypeError): subobject(1)
The
mypyerror in the comment above is useful and should be raised even after this issue is fixed. It should probably get a better message. Something like:error: "an_object" cannot be used as a base class because its type is "object", which is not a subclass of "type"On the other hand, if we fix the problem, the runtime error will be detected ahead of time by mypy as well.
def subclass(a_type: type) -> type: class A(a_type): pass # error: Invalid type "a_type", Invalid base class return A def test_subclass() -> None: assert isinstance(Exception, type) assert isinstance(subclass(Exception), type) assert issubclass(subclass(Exception), Exception) assert isinstance(1, object) with pytest.raises(TypeError): subclass(1) # error: Argument 1 to "subclass" has incompatible type "int"; expected "type"
The code above should only have one type error (the last line) as the error on the second line is caused by the current issue.
I'll add here another case, which I believe is related, even though I'm not sure to understand all the whys and wherefores.
from typing import Type class A: pass a = A a2: Type[A] = A # Ok class B(a): pass # Ok class B2(a2): pass # Errors: not valid as a type; Invalid base class def f(cv: Type[A]): class B3(cv): pass # Errors: not valid as a type; Invalid base class return B3 f(a) # Ok f(a2) # Ok
I would expect that if I promise the type checker that my variable is of the class type, it would accept it as base class. Is there some subtleties I don't get?
@frankie567
a = Acreates a type alias. mypy doesn't allow these to rebound and they are special in some other ways.a2: Type[A] = Acreates a variable, and it could be assigned a new value elsewhere. In particular, it could be bound to a subclass ofAthat mypy doesn't know anything about.That's why mypy complains about using
a2as a base class -- for example, mypy can't bindsuper().__init__correctly sincea2could refer to a subclass ofAthat changes the signature of__init__. Also, method overrides can't be checked, since there may be additional methods mypy doesn't see.Reacted by François Voron@JukkaL Thank you for this explanation, I understand now 🙂Sorry for the noise on the ticket!
This does not only affect bases from args, but also the local scope / function body.
I've looked a bit into why the following is not working, and it appears to be due to the fact that
configure_base_classesdoes not know aboutbase:Lines 1472 to 1477 in a94e649
msg = 'Invalid base class' name = self.get_name_repr_of_expr(base_expr) if name: msg += ' "{}"'.format(name) self.fail(msg, base_expr) info.fallback_to_any = True There is some code in
analyze_function_bodyalready to bind things into the local scope, but it appears to not work here correctly / as expected?:Lines 897 to 917 in a94e649
def analyze_function_body(self, defn: FuncItem) -> None: is_method = self.is_class_scope() with self.tvar_scope_frame(self.tvar_scope.method_frame()): # Bind the type variables again to visit the body. if defn.type: a = self.type_analyzer() a.bind_function_type_variables(cast(CallableType, defn.type), defn) self.function_stack.append(defn) self.enter(defn) for arg in defn.arguments: self.add_local(arg.variable, defn) # The first argument of a non-static, non-class method is like 'self' # (though the name could be different), having the enclosing class's # instance type. if is_method and not defn.is_static and not defn.is_class and defn.arguments: defn.arguments[0].variable.is_self = True defn.body.accept(self) self.leave() self.function_stack.pop() import typing # works base = object class C(base): pass def f1() -> None: base = object class C(base): # Variable "base" is not valid as a type / Invalid base class "base" pass def f2(base: typing.Any) -> None: class C(base): # Variable "base" is not valid as a type / Invalid base class "base" pass # works def f3(base: typing.Any) -> None: base2: typing.Any = base class C(base2): pass
mypy 0.750+dev.a94e649de794ecd169a5bcc42274e5205ffbb1fb
Any update on this issue? It's quite a big one IMO.
Reacted by Andy , Luca Soldaini, Pedro Fonini, Sam Pegler, Lukas Juhrich, Daniel Fainberg, monkeyusage, Alex Ray, cristiciortea, Eugene Korostylov and 8 moreOne more case, very similar to @blueyed's, but in the context of a classmethod -- and I also found a third incorrect error message (name-defined) that I don't think was mentioned yet in this thread: even though
clshas typetype[C]whereCis bounded byMyClass, mypy is unable to resolve the reference tocls.InnerClass.from typing import TypeVar C = TypeVar("C", bound="MyClass") class MyClass: class InnerClass: pass @classmethod def dynamic_subclass(cls: type[C]) -> type[C]: # Mypy emits two errors on the following line: # error: Variable "cls" is not valid as a type [valid-type] # error: Invalid base class "cls" [misc] class MySubClass(cls): # Mypy emits one error on the following line: # Name "cls.InnerClass" is not defined [name-defined] class MySubInnerClass(cls.InnerClass): pass pass return MySubClass # to show that everything works as expected at runtime: SubClass = MyClass.dynamic_subclass() my_subclass_instance = SubClass()
- added a commit that references this issue
on Nov 21, 2022
Class
Din the example below is rejected, even though classE, which looks very similar, is accepted:The above example shouldn't generate errors.
(Originally reported by @germaniumhq in #2477 (comment).)