0

The following code produces the error message:

  1. Operator "|" not supported for types "set[str]" and "set[str]"

......   Operator "|" not supported for types "set[str]" and "set[str]" when expected type is "Iterable[LiteralString]"

def update_no_proxy():
    def _normalize_url(url: str) -> str:
        return urlparse(url).netloc

    old_no_proxy_urls = set(os.getenv('NO_PROXY', '').split(','))
    no_proxy_new_urls = {_normalize_url(url) for url in NO_PROXY_URLS}
    no_proxy_new_urls = {*no_proxy_new_urls, get_local_ip_address()}
    no_proxy_urls: str = ','.join(old_no_proxy_urls | no_proxy_new_urls)
    for env_name in NO_PROXY_ENV_NAMES:
        os.environ[env_name] = no_proxy_urls.strip(', ')

However, the following code does not produce the error:

def update_no_proxy():
    def _normalize_url(url: str) -> str:
        return urlparse(url).netloc

    old_no_proxy_urls = set(os.getenv('NO_PROXY', '').split(','))
    no_proxy_new_urls = {_normalize_url(url) for url in NO_PROXY_URLS}
    no_proxy_new_urls = {*no_proxy_new_urls, get_local_ip_address()}
    all_no_proxy_urls = old_no_proxy_urls | no_proxy_new_urls
    no_proxy_urls = ','.join(all_no_proxy_urls)
    for env_name in NO_PROXY_ENV_NAMES:
        os.environ[env_name] = no_proxy_urls.strip(', ')

Why putting all_no_proxy_urls in a new variable solves the problem?

Yam Mesicka
  • 6,243
  • 7
  • 45
  • 64
  • 1
    I don't get any errors from pyright for the first version of the code (using pyright 1.1.279 and python 3.11.1). I've tried annotating `NO_PROXY_URLS` and `NO_PROXY_ENV_NAMES` as both `list[str]` and `set[str]`, and in both cases pyright seems happy. – larsks Jan 05 '23 at 13:24
  • I'm currently using pyright 1.1.287 and Python 3.10.9 (fixed the tagging of the python version, sorry for that). `NO_PROXY_URLS` is `tuple[str]` – Yam Mesicka Jan 05 '23 at 13:33

0 Answers0