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 — `fnmatch.filter` and `filterfalse` work with pathlikes only on non-Posix platforms · Issue #160900 · python/cpython

fnmatch.filter and filterfalse work with pathlikes only on non-Posix platforms #160900

New issue
Copy link
New issue
Copy link
Open
#160980
Open
fnmatch.filter and filterfalse work with pathlikes only on non-Posix platforms#160900
#160980
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@JelleZijlstra
JelleZijlstra
opened on Oct 29, 2026
Issue body actions

fnmatch.filter contains a check:

    if os.path is posixpath:
        # normcase on posix is NOP. Optimize it away from the loop.

It calls normcase on the paths to be filtered only on non-Posix platforms.

The check dates from 2001 and the comment was probably true then. But nowadays, os.path.normcase does do something even on Posix: it normalizes path-like objects into strs or bytes.

So on MacOS I get:

>>> import fnmatch
>>> from pathlib import Path
>>> fnmatch.filter([Path("hi")], "hi")
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    fnmatch.filter([Path("hi")], "hi")
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/Users/jelle/py/cpython/Lib/fnmatch.py", line 61, in filter
    if match(name):
       ~~~~~^^^^^^
TypeError: expected string or bytes-like object, got 'PosixPath'

But this will work on Windows.

I think this is should be fixed for two reasons:

  • It's an unnecessary difference in behavior between platforms. Python should work the same across OSes as much as possible, and there's no good reason for the behavior to differ here.
  • In general, we want path-related functions to accept path-like objects (PEP 519)

However, we've generally treated PEP 519 changes as new features, so let's change this on main only.

Discovered in python/typeshed#16647

Linked PRs

  • gh-160900: support path-like objects in fnmatch filters #160980

Activity

  1. JelleZijlstra
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    type-featureA feature request or enhancementA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Oct 29, 2026
  2. picnixz commented on Oct 29, 2026

    @picnixz
    picnixz
    on Oct 29, 2026
    Member
    More actions

    Yeah, this is somethign I found when I implemented filterfalse but I was afraid to change the implementation. I don't think we need this small optimization anymore.

  3. zedzhen commented on Oct 29, 2026

    @zedzhen
    zedzhen
    on Oct 29, 2026
    More actions

    fnmatch.fnmatch normalizes both parameters (this is explicitly stated in the documentation), and the documentation says

    fnmatch.filter is the same as [n for n in names if fnmatch(n, pat)], but implemented more efficiently.

  4. MonikaDvorackova
    added 5 commits that reference this issue on Oct 30, 2026

    pythongh-160900: add NEWS entry

    feb3718

    pythongh-160900: document path-like support and test mixed filename t…

    e32415c

    pythongh-160900: support path-like objects in fnmatch filters

    ab8c256

    pythongh-160900: normalize path-like names in filterfalse

    6fe66e6

    pythongh-160900: Remove redundant fspath calls

    584ec30
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

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — 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.