TypeError: 'type' object is not subscriptable
locations: dict[str, float] = { "z": -1.38, 'anotherkey': .....
locations: dict[str, float] = { "z": -1.38, 'anotherkey': .....
위와 같은 dict[str, float]에서 Type object is not subscriptable 에러가 났다
찾아보니까 3.8버전의 파이썬까지는 위와 같은 type을 automatic하게 알아듣는 기능이 구현 안되어 있다고 한다.
python 3.9 이상으로 upgrade
현재 python 3.8을 사용하고 있는데, 이문제를 해결하기 위한 가장 쉬운 방법은
env를 3.9이상으로 다시 만드는 것이다.
conda env create -n envaname python=3.10
Typing에서 직접 자료형을 가져오기
from typing import Dict
locations: Dict[str, float] = {
"z": -1.386496e03,
# Add other entries here as needed
}
위와 같이 dict -> Dict로 대문자형으로 바꾸면 된다.
dict는 못 알아본다
def normalise_atmos_var(
x: torch.Tensor,
name: str,
atmos_levels: tuple[int | float, ...],
unnormalise: bool = False,
) -> torch.Tensor:
비슷한 다른 예시를 가져와보면 이 경우에서도
tuple 같은 소문자 [] 를 못 알아본다
from typing import Tuple, Union
import torch
def normalise_atmos_var(
x: torch.Tensor,
name: str,
atmos_levels: Tuple[Union[int, float], ...],
unnormalise: bool = False,
위와 같이 Tuple, Union을 가져와서 사용해야, python 3.8 에서도 사용할 수 있다.
Reference
https://stackoverflow.com/questions/75202610/typeerror-type-object-is-not-subscriptable-python
TypeError: 'type' object is not subscriptable Python
Whenever I try to type-hint a list of strings, such as tricks: list[str] = [] , I get TypeError: 'type' object is not subscriptable. I follow a course where they use the same code, but it works fo...
stackoverflow.com