当返回类型为 TypeVar 时返回子类

时间:2021-07-24 11:08:39

标签: python type-hinting mypy python-typing

我有以下文件 <SafeAreaView style ={styles.container}> <Text style ={styles.title}>New Hot Tokens</Text> <FlatList data={listItems} renderItem={({item}) => <Text style={styles.item} onPress={() => { navigation.navigate('Details', {coinID: (item) }); }} >{`${item.key}`}</Text>}/> </SafeAreaView> ); } function DetailsScreen({route, navigation}) { const { coinID } = route.params; console.log(coinID.key) const { loading: coinLoading, error, data: coinData } = useQuery(COIN_INFO, { variables: { coinForQuery: coinID.key}, pollInterval: 15000, fetchPolicy: 'network-only', notifyOnNetworkStatusChange: true } );

t.py

,哪个类型检查正常:

from typing import overload, TypeVar, List, Any, Iterable

class Animal: ...
class Dog(Animal): ...

T = TypeVar('T', bound=Animal)

def foo(self: T) -> T:
    return self

但是,如果我将其更改为:

$ mypy t.py 
Success: no issues found in 1 source file
(venv) marco@marco-Predator-PH315-52:~/tmp$

然后我收到一个输入错误:

from typing import overload, TypeVar, List, Any, Iterable

class Animal: ...
class Dog(Animal): ...

T = TypeVar('T', bound=Animal)

def foo(self: T) -> T:
    if isinstance(self, Dog):
        return self
    else:
        return self

为什么会这样,如果我有一个 $ mypy t.py t.py:10: error: Incompatible return value type (got "Dog", expected "T") Found 1 error in 1 file (checked 1 source file) 块,我怎样才能使它类型检查正常?

0 个答案:

没有答案