GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Provide a better way to allow static type-narrowing for the UPath constructor · Issue #446 · fsspec/universal_pathlib

Provide a better way to allow static type-narrowing for the UPath constructor #446

New issue
Copy link
New issue
Copy link
Closed
#591
Closed
Provide a better way to allow static type-narrowing for the UPath constructor#446
#591
Copy link
Labels
enhancement 🚀New feature or request

Description

@ap--
ap--
opened on Oct 8, 2025
Issue body actions

Currently it seems all python type-checkers seem to have some limitation regarding either __new__ overloads or type.__call__ overloads.

see: #435

This issue tracks if we can come up with a better way to overload the constructor thats supported by all typecheckers.

xref / blocked-by:

  • respect return type of __new__ astral-sh/ty#281
  • Honor return type of __new__ even if not a subclass python/mypy#15182
  • overloaded __new__ ignored when introducing __init__ python/mypy#17251

Activity

  1. ap--
    added
    enhancement 🚀New feature or request
    on Oct 8, 2025
  2. ap-- commented on Oct 16, 2025

    @ap--
    ap--
    on Oct 16, 2025
    CollaboratorAuthor
    More actions

    There are two ways right now to get IDE autocompletion and suggestions for storage_options:

    1. use the correct upath subclass directly
      Image

    2. use upath.registry.get_upath_class to narrow to the right class on protocol
      Image

  3. ap-- commented on Apr 9, 2026

    @ap--
    ap--
    on Apr 9, 2026
    CollaboratorAuthor
    More actions

    I should revisit this. ty now has support for it.

  4. ap--
    added 2 commits that reference this issue on Nov 23, 2026

    ci: check UPath constructor narrowing with mypy, pyright and ty

    9f5f322

    Fix UPath constructor type narrowing in pyright and ty (#591)

    daaf6b5
  5. ap--
    closed this as completedin #591on Nov 23, 2026
  6. ap-- commented on Nov 23, 2026

    @ap--
    ap--
    on Nov 23, 2026
    CollaboratorAuthor
    More actions

    I finally got back to this. Short version: most of the remaining problem was on our side, not in the type checkers 🙈

    Upstream status:

    • respect return type of __new__ astral-sh/ty#281 was fixed in April ([ty] respect __new__ and metaclass __call__ return types astral-sh/ruff#24357), so ty respects __new__ return types now
    • Honor return type of __new__ even if not a subclass python/mypy#15182 was fixed by Respect explicit return type of __new__() python/mypy#21441, released in mypy 2.2.0
    • overloaded __new__ ignored when introducing __init__ python/mypy#17251 (overloaded __new__ + __init__) is still open, but our class layout already sidesteps it. The overloads are on UPath.__new__, __init__ comes from the mixin, so mypy uses __new__

    So why did pyright and ty still infer UPath for UPath("b/k", protocol="s3")? Two reasons:

    1. The overload return types were written as _uimpl.cloud.S3Path via import upath.implementations as _uimpl. upath/implementations/__init__.py doesn't import its submodules, so pyright resolved the return type to Unknown ("cloud" is not a known attribute of module), and ty warned about a possibly-missing submodule. An Unknown return from __new__ falls back to the class itself, which is UPath. mypy is more lenient here, so we never noticed.
    2. ty uses the return type of our annotated metaclass __call__(cls: type[T], ...) -> T and stops there. pyright and pyrefly continue with __new__.

    #591 fixes both. It imports the implementation classes directly (like upath.registry already does) and hides _UPathMeta.__call__ from type checkers (no runtime change). Results with mypy 2.4.1 / 1.19.1, pyright 1.1.414, ty 0.0.99 and pyrefly 1.4.1:

    from upath import UPath
    from upath.implementations.cloud import S3Path
    from upath.registry import get_upath_class
    
    UPath("s3://bucket/key")                       # UPath everywhere (URIs can't be parsed statically)
    UPath("bucket/key", protocol="s3")             # S3Path: mypy, pyrefly | now also pyright, ty
    UPath("bucket/key", protocol="s3", **opts)     # S3Path: same as above
    UPath("bucket/key", protocol="s3", anon="x")   # error only in pyright (it checks S3Path.__init__)
    S3Path("s3://bucket/key", anon=True)           # S3Path + storage options checked everywhere*
    get_upath_class("s3")("bucket/key", anon=True) # S3Path + storage options checked everywhere*

    * ty checks option types, but by design it accepts unknown keys for (open) TypedDicts, see astral-sh/ty#4212.

    Making pyright check __init__ through the narrowed type also uncovered three wrong protocol literals in our __init__ stubs (TarPath said "zip", SFTPPath didn't accept "ssh", WebdavPath didn't accept "webdav"). Those are fixed in the PR too.

    To stop this from silently regressing again, the PR adds a small nox -s type-narrowing CI job. It runs mypy, pyright and ty on an assert_type file covering all literal protocols. On current main that file produces 32 errors each in pyright and ty.

    Two known limitations:

    • Only pyright validates storage options passed through UPath(..., protocol=...). To get autocompletion and checks for storage options in every checker, use the implementation class or get_upath_class(...) as described above.
    • The overloads are inherited by subclasses, so S3Path("b/k", protocol="gcs") (which raises at runtime) is now inferred as GCSPath by pyright/ty instead of being flagged. If that becomes a problem, we can add a TYPE_CHECKING-only __new__ to the implementation classes.

    I'll close this once the PR is released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement 🚀New feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.