English 中文(简体)
为什么在争论中使用通用的自我类型不安全?
原标题:Why is it unsafe to use a generic self type in an argument?
The mypy doc says: Note that mypy lets you use generic self types in certain unsafe ways in order to support common idioms. For example, using a generic self type in an argument type is accepted even though it’s unsafe. It then mentions an example of using generic self type in an argument: from typing import TypeVar T = TypeVar("T") class Base: def compare(self: T, other: T) -> bool: return False class Sub(Base): def __init__(self, x: int) -> None: self.x = x # This is unsafe (see below) but allowed because it s # a common pattern and rarely causes issues in practice. def compare(self, other: Sub) -> bool: return self.x > other.x b: Base = Sub(42) b.compare(Base()) # Runtime error here: Base object has no attribute x I can t understand what s the "unsafe" matter here. The main problem is that the b variable is assigned a type that is not precise (b:Base) So what is the point in the doc? P.S: The point of "unsafe" in the doc is that it type checks well but will produce an exception in runtime. But what I can t understand is that why this is related to the annotation of other: T in Base.compare. I think It s due to the imprecise type of b that should have been cast to Sub by the developer. In other words: What should I do in case I have a method that accepts the same object type as self as its other arguments?
问题回答
Base.compare only requires that both arguments have the same static type. The static type of b is Base (due to the hint), despite that its dynamic type is Sub at runtime. When type-checking b.compare(Base()), everything "checks": b has static type Base. In Base.compare, self has static type T, which can accept anything, so T is bound to Base. The explicit argument to Base.compare is also of type Base, so is allowed by the type checker. At no point did you specify that either self or other requires support for an x attribute, leading to the runtime error. It s not really a "problem" that b is assigned a value of type Sub. Sub is a subclass of Base, and thus is compatible with the hinted type Base.




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签