You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
def make_obj_2(cls: Type[Base]=Foo) -> Base: # -- line 23
inst = cls()
return inst
class Bar:
foo = None # type: Base -- line 29
subtype.py: note: In function "make_obj_2":
subtype.py:23: error: Incompatible types in assignment (expression has type "Foo", variable has type Type[Base])
subtype.py: note: At top level:
subtype.py:29: error: Invalid type "subtype.Base"
IIRC we have another bug for this issue already. IMO mypy should not forbid
construction of an abstract class if the class is an argument declared
using Type[] -- it should assume you are passing it a concrete class. But
maybe we need a check that you call this thing with a concrete subclass;
and for that we would need some additional notation (unless maybe we could
just say that whenever there's an argument of Type[A] where A is abstract,
that the argument must be a concrete subclass. But for that we'd need some
experiment to see if there's much real-world code that passes abstract
classes around. (If there is, we'd need to have a way to indicate the need
in the signature.)
I just stumbled upon this while implementing a generic parser over concrete types of an abstract base class. So, there is an actual real-world use case for this.
Basically, the following should type check flawlessly in my opinion:
I just stumbled upon this while implementing a generic parser over
concrete types of an abstract base class. So, there is an actual real-world
use case for this.
Basically, the following should type check flawlessly in my opinion:
@elazarg Mypy doesn't need to flag everything that looks unsafe. It must be a useful tool, and some code that is theoretically unsafe is both common and useful in practice and the unsafe scenario never occurs, so complaining about it is unhelpful. In general there is no single concrete subclass of an ABC like Base in the example, so there is no easy way to state "any concrete subclass of Base".
I would much rather have an occasional unsafe piece of code pass without error (a false negative) than require users to add extra markup to code that is almost always fine in practice (a false positive), if the annoyance of false positives outweighs the cost of false negatives. This is one of those cases. This is a different philosophy from most typed languages, and it is Python's runtime type checking that gives us this luxury.
I am not sure that in practice unsafe scenarios never occur. It is not unlikely to forget adding an override to a new abstract method, and this is the place where it can be caught.
I understand the reasoning however: it is unsafe, but not yet possible to express in a useful way, so we err towards flexibility, avoiding false positives. Fair enough.
(The reason I say "yet" relates to my pending proposal in python-ideas. An easy and clear way to write it would be
Activity
sirkonst commentedon Sep 22, 2016
I guess I found solution:
But maybe open new bugs:
refi64 commentedon Sep 22, 2016
What if you do:
gvanrossum commentedon Sep 22, 2016
aomader commentedon Sep 23, 2016
I just stumbled upon this while implementing a generic parser over concrete types of an abstract base class. So, there is an actual real-world use case for this.
Basically, the following should type check flawlessly in my opinion:
gvanrossum commentedon Sep 23, 2016
Agreed.
--Guido (mobile)
On Sep 23, 2016 7:58 AM, "Oliver Mader" [email protected] wrote:
elazarg commentedon Sep 23, 2016
I don't understand why should it typecheck. It looks unsafe to me.
gvanrossum commentedon Sep 23, 2016
@elazarg Mypy doesn't need to flag everything that looks unsafe. It must be a useful tool, and some code that is theoretically unsafe is both common and useful in practice and the unsafe scenario never occurs, so complaining about it is unhelpful. In general there is no single concrete subclass of an ABC like Base in the example, so there is no easy way to state "any concrete subclass of Base".
I would much rather have an occasional unsafe piece of code pass without error (a false negative) than require users to add extra markup to code that is almost always fine in practice (a false positive), if the annoyance of false positives outweighs the cost of false negatives. This is one of those cases. This is a different philosophy from most typed languages, and it is Python's runtime type checking that gives us this luxury.
elazarg commentedon Sep 24, 2016
I am not sure that in practice unsafe scenarios never occur. It is not unlikely to forget adding an override to a new abstract method, and this is the place where it can be caught.
I understand the reasoning however: it is unsafe, but not yet possible to express in a useful way, so we err towards flexibility, avoiding false positives. Fair enough.
(The reason I say "yet" relates to my pending proposal in python-ideas. An easy and clear way to write it would be
It is easily understood but unfortunately not reasonable to use as things currently stand)
gvanrossum commentedon Sep 24, 2016
This is a dup of #1843.