Mypy falsely reports error on union-typed variable when branching on type

旧街凉风 提交于 2019-12-01 23:42:39

问题


I ran into an issue when using mypy and have been able to find any help/reports regarding it. The following simplified code and error message should be self-explanatory:

#!/usr/bin/env python3

from typing import List, Union

class Corpus:
  path: List[str]

  def __init__(self, path:Union[str,List[str]]) -> None:
    if type(path) == str:
      self.path = [path]
    else:
      self.path = path

Mypy gives the following errors:

simplified.py:10: error: List item 0 has incompatible type "Union[str, List[str]]"; expected "str"
simplified.py:12: error: Incompatible types in assignment (expression has type "Union[str, List[str]]", variable has type "List[str]")

Even though the type of path variable is checked so that self.path should always result in string list, mypy complains about incompatible types.

Am I overlooking something or is this a bug in mypy? (It it is a bug, should I go with #type: ignore annotation or is there a better work-around?)

(Some background: I decided to ease my life by writing a module which would take care of some repeating work. The argument in question should be a path to text data and I expect it to be only one string most of the time so I don't want to force putting it in a list. However, I wish to allow specifying more paths too. Internally, I store it as a list anyway as iterator over the class is always initialized with such list (and then possibly extends it further by "unpacking" directories)).


回答1:


Try using isinstance(path, str) instead of type(path) == str. The former makes mypy typecheck your code without reporting an error.

Mypy really ought to support the latter form though -- there's an open feature request about it. The reason why this isn't implemented yet is almost certainly due to lack of time -- mypy's core team is pretty small, and there's an easy workaround in this case, so the feature was deprioritized.

(But hey, mypy is open source, so if you have some spare time...)



来源:https://stackoverflow.com/questions/51656300/mypy-falsely-reports-error-on-union-typed-variable-when-branching-on-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!