Skip to content

build_master red on every push since 2026-07-12: mypy 2.0 rejects the re-annotated format in examples/bench_diskless.py:56 #1498

Description

@glaziermag

The Build and Test on Linux with netcdf-c github master workflow has failed on every master push since 2026-07-12 (first red run 29205839185, latest 35113547766 on 09-16; last green 24944443641 on 04-26). The netcdf-c-master test step passes; the red is the mypy examples command in the Stubtest step:

examples/bench_diskless.py:56: error: Name "format" already defined on line 43  [no-redef]
examples/bench_diskless.py:60: error: Argument "format" to "write_netcdf" has incompatible type "str"; expected "Literal['NETCDF4', 'NETCDF4_CLASSIC', 'NETCDF3_CLASSIC', 'NETCDF3_64BIT_OFFSET', 'NETCDF3_64BIT_DATA']"  [arg-type]
Found 2 errors in 1 file (checked 14 source files)

Cause. for format in [...] at line 43 defines format as str; line 56 re-annotates the same name as Literal["NETCDF3_CLASSIC"]. The file has not changed since 2024-08, and the only master commits between the last green and the first red are dependabot action bumps. What changed is mypy: 2.0.0 (released 2026-05-06, installed unpinned by pip install --group dev) gave allow_redefinition = true (set in pyproject.toml) the new semantics, which per the 2.0 changelog "doesn't allow giving two type annotations for the same variable". Once the annotation is rejected, format stays str, so the write_netcdf(format=format) call on line 60 fails as well. Locally, mypy 1.20.2 passes the file and 2.0.0/2.1.0/2.2.0/2.3.1 all report the two errors.

Reproduce

pip install mypy==2.3.1
mypy examples/bench_diskless.py

Fix (one file): give the second variable its own name so the Literal annotation the call needs is kept.

-format: Literal["NETCDF3_CLASSIC"] = 'NETCDF3_CLASSIC'  # mypy should know this but it needs help...
+fmt: Literal["NETCDF3_CLASSIC"] = 'NETCDF3_CLASSIC'  # mypy should know this but it needs help...
 trials=50
-sys.stdout.write('test caching of file in memory on open for %s\n' % format)
-sys.stdout.write('testing file format %s ...\n' % format)
-write_netcdf('test1.nc',format=format,closeit=True)
+sys.stdout.write('test caching of file in memory on open for %s\n' % fmt)
+sys.stdout.write('testing file format %s ...\n' % fmt)
+write_netcdf('test1.nc',format=fmt,closeit=True)

#1486 removed the annotation instead; that clears no-redef but format is then str, so the arg-type error on the next line remains. The other option is allow_redefinition_old = true in [tool.mypy], which mypy keeps "on a best effort basis".

On my fork the workflow at master (6e67576) fails the same way (run 35813882014) and passes with the diff above (run 35813920703, commit glaziermag/netcdf4-python@52a6a41). I can turn this into a PR if you prefer.

Written with the help of an AI agent (Claude); the runs and the mypy version checks above were carried out as described.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions