Skip to content

urllib.request.urlopen() type is unknown #2221

@user202729

Description

@user202729

Describe the Bug

Consider the following code

import shutil
import urllib.request as request
with request.urlopen("https://duckduckgo.com") as remote, open("main.html", 'wb') as local:
    shutil.copyfileobj(remote, local)

It runs successfully at runtime, but pyrefly gives an error

ERROR `Buffer` is not assignable to upper bound `bytes | str` of type variable `AnyStr` [bad-specialization]
 --> a.py:4:23
  |
4 |     shutil.copyfileobj(remote, local)
  |                       ^^^^^^^^^^^^^^^
  |
 INFO 1 error

This is because remote has type Unknown (Any in mypy). For comparison, mypy is more tolerant, and if the argument is Any, it will not type-check this argument.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIEAtsbgE4AuABHABYCuD0AOutbYybs6UKBGyE6MAI7t4zVHCZTZ83gHcIDVsply4DQsKi5iMdAApuIVgwbE4iAPRPM7AMYBrN14DmuQndcKmsASiZFXSpcBhgAGiZTcysQKlQIdEJbKihrBIBydWx88MiTd1QoRF4mWpYOLihA01JIWFxsACsLKWjYhPLK0N4QOJBOaDgSckQQAGImAFVGrVImMHZ0dy5cdDheXkwYMHX6NIYAfXR2KmwYOgt8RCYMhnCAWgA%2BFgY6avQ6roGMIAWBrAA5G53P5MYD4AC%2B1hGYzIUjAUFIhAYwSgFAWAAVSGiMSwMDgCEwguhIL5hKgdpleAsAMowGBMWz2RwuVHHDGEei%2BJzmVy4dxwJxUml0hlOU50CIAN3SUFQ2FglN20ro9Igu0SxAZU14ZG0u3eivucD1AIAvExrABmQgARgATEj0CB4WNUNsIJaAGLQGAUNBYPBEMjeoA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions