DEV Community

Tohru
Tohru

Posted on

Why fnmatchcase Matters for Cross-Platform Python Tools

In toml-sort, a configuration override for servers.Beta could also apply to servers.beta on Windows. On Linux, it didn't.

The input file and configuration were identical. The difference came from Python's fnmatch.fnmatch(), which toml-sort used to match TOML key paths.

An Override That Shouldn't Match

toml-sort lets you override sorting options for individual TOML tables. For example, this configuration enables key sorting globally but disables it for servers.Beta:

[tool.tomlsort]
sort_table_keys = true

[tool.tomlsort.overrides."servers.Beta"]
table_keys = false
Enter fullscreen mode Exit fullscreen mode

Now consider a table whose name differs only in case:

[servers.beta]
z = 1
a = 2
Enter fullscreen mode Exit fullscreen mode

TOML keys are case-sensitive. The override for servers.Beta shouldn't apply to servers.beta, so the keys should be sorted:

[servers.beta]
a = 2
z = 1
Enter fullscreen mode Exit fullscreen mode

With toml-sort 0.25.0, the original order could remain unchanged on Windows because the override matched despite the case difference. On Linux, the keys were sorted.

This could also affect toml-sort --check: the same file and configuration might pass the check on one platform and fail on another.

How fnmatch() Caused the Difference

Python's fnmatch.fnmatch() supports shell-style wildcard matching. Before matching, it passes both the input string and pattern through os.path.normcase().

On Windows, normcase() converts uppercase characters to lowercase. On Linux, it leaves the case unchanged.

For example:

import fnmatch

fnmatch.fnmatch("servers.beta", "servers.Beta")
Enter fullscreen mode Exit fullscreen mode

This returns True on Windows and False on Linux.

That behavior is documented, but toml-sort wasn't matching filenames. It was matching dotted TOML key paths, whose case-sensitivity shouldn't depend on the operating system.

The relevant code was in _find_config_override().

It first looked for an exact match:

path_string = ".".join(path)

if path_string in self.sort_config_overrides:
    return self.sort_config_overrides.get(path_string)
Enter fullscreen mode Exit fullscreen mode

If that failed, it tried the configured patterns:

matches = [
    config
    for pattern, config in self.sort_config_overrides.items()
    if fnmatch.fnmatch(path_string, pattern)
]
Enter fullscreen mode Exit fullscreen mode

The exact lookup was already case-sensitive. The fallback wasn't consistently so.

Even servers.Beta, which contains no wildcard, could match servers.beta through that fallback on Windows.

The fix in PR #110 changed one function call:

- if fnmatch.fnmatch(path_string, pattern)
+ if fnmatch.fnmatchcase(path_string, pattern)
Enter fullscreen mode Exit fullscreen mode

Unlike fnmatch(), fnmatchcase() doesn't normalize the strings through os.path.normcase(). It performs case-sensitive matching regardless of the host platform.

Wildcard support and the existing exact-match priority were preserved.

The fix was released in toml-sort 0.26.0 on November 14, 2026.

Reproducing Windows Matching on Linux

The regression test needed to catch a Windows-specific problem even when running on Linux.

A normal case-sensitivity test on Linux wouldn't have done that. The original fnmatch() call already behaved correctly there.

Instead, the test uses ntpath, Python's Windows-oriented path module, together with pytest's monkeypatch fixture:

import ntpath
import os

monkeypatch.setattr(os.path, "normcase", ntpath.normcase)
Enter fullscreen mode Exit fullscreen mode

This replaces the normalization function used by fnmatch() with the Windows implementation.

With the original code, patterns that differ only in case can now match on Linux as they would on Windows. The updated fnmatchcase() call is unaffected.

The upstream test covers five patterns against the TOML path servers.beta:

Pattern Expected match
servers.beta Yes
servers.* Yes
servers.Beta No
Servers.* No
servers.[A-Z]* No

The last case checks a case difference inside a character class, rather than ordinary text.

The test also checks toml-sort's sorting behavior. It creates a small TOML document with unsorted keys and supplies an override that disables sorting when the pattern matches.

If the pattern should match, the keys must remain in their original order. Otherwise, they must be sorted.

Before the fix, the simulated Windows normalization made the three case-mismatched patterns select the wrong override. With fnmatchcase(), all five expectations are satisfied.

The test doesn't emulate an entire Windows environment. It replaces the specific normalization step responsible for this bug.

That makes the platform-dependent failure visible to Linux CI without needing a Windows runner for this particular check.

Matching the Data, Not the Platform

fnmatch() isn't inherently the wrong function. Its platform-dependent normalization can be useful when that's the intended matching behavior.

For TOML identifiers, however, it introduced a dependency on the operating system that the format itself doesn't have.

In this case, switching to fnmatchcase() made override selection consistent, and the regression test checked that consistency through the resulting sorting behavior.


References

Top comments (0)