google / pytype

A static type analyzer for Python code

Home Page:https://google.github.io/pytype

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pytype seems to ignore Union type

mlsad3 opened this issue · comments

When I try passing the http.cookiejar.MozillaCookieJar to requests.get(), it seems to trip up on the typing for the requests.get().

The requests.get() is looking for: cookies: Optional[Union[MutableMapping[str, str], requests.cookies.RequestsCookieJar]]. And http.cookiejar.MozillaCookieJar fits the requirement for requests.cookies.RequestsCookieJar. But when I run pytype, it fails saying it needs to be MutableMapping[str, str].

#!/usr/bin/env python3

import http.cookiejar
import requests

cookie_jar = http.cookiejar.MozillaCookieJar('~/.gitcookies')
cookie_jar.load()

requests.get('https://wikipedia.org', cookies=cookie_jar)

Gives

FAILED: ./test.pyi 
./dv_env/bin/python -m pytype.single --imports_info .pytype/imports/infratools.gerrit.test.imports --module-name infratools.gerrit.test --platform linux -V 3.8 -o .pytype/pyi/infratools/gerrit/test.pyi --analyze-annotated --enable-cached-property --nofail --quick ./test.py
File "./test.py", line 9, in <module>: Function requests.api.get was called with the wrong arguments [wrong-arg-types]
         Expected: (url, params, *, allow_redirects, auth, cert, cookies: Optional[Union[MutableMapping[str, str], requests.cookies.RequestsCookieJar]] = ..., ...)
  Actually passed: (url, cookies: http.cookiejar.MozillaCookieJar)
  Attributes of protocol MutableMapping[str, str] are not implemented on http.cookiejar.MozillaCookieJar: __delitem__, __getitem__, __setitem__

For more details, see https://google.github.io/pytype/errors.html#wrong-arg-types
ninja: build stopped: subcommand failed.

No, I think I'm wrong here. Ducktyping allows the code to work fine, but I'm not seeing how MozillaCookieJar inherits from RequestsCookieJar.