google / pytype

A static type analyzer for Python code
https://google.github.io/pytype
Other
4.77k stars 278 forks source link

PyType permits usage of two different types for one TypeVar. #1557

Open Frang84 opened 10 months ago

Frang84 commented 10 months ago

Pytype permits one typeVar (T) for being a list of strings and list of integers.

T = TypeVar("T")

def functionTab(input_list: List[T], addend: T) -> List[T]: 
  output_list: List[T] = []
  for i in input_list:
    output_list.append(i + addend)
  return output_list

test_list :List[List[int]] = [[1,2,3], [4,5,6]]
result = functionTab(test_list, ["abcd"])

OUTPUT: Success: no errors found

rchen152 commented 10 months ago

Also happens with Sequence and Iterable:

T = TypeVar("T")
def functionTab(first: T, second: T) -> T : 
  resultList = []
  for i in first:
    resultList.append(i)
  for i in second:
    resultList.append(i)
  return resultList  

list1 : Sequence[str] = ["dog", "cat"]
list2 : Sequence[int] = [1,2,3]

functionTab(list1, list2)
def functionTab(first: T, second: T) -> T : 
  resultList = []
  for i in first:
    resultList.append(i)
  for i in second:
    resultList.append(i)
  return resultList  

list1 : Iterable[int] = [1,2,3]
list2 : Iterable[str] = ["dog", "cat"]

functionTab(list1, list2)