Skip to content

Commit 246817b

Browse files
committed
mypy: fix all assignment
1 parent 248ee13 commit 246817b

File tree

9 files changed

+40
-25
lines changed

9 files changed

+40
-25
lines changed

distutils/archive_util.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66
from __future__ import annotations
77

88
import os
9+
from types import ModuleType
910
from typing import Literal, overload
1011

11-
try:
12-
import zipfile
13-
except ImportError:
14-
zipfile = None
15-
16-
1712
from ._log import log
1813
from .dir_util import mkpath
1914
from .errors import DistutilsExecError
2015
from .spawn import spawn
2116

17+
zipfile: ModuleType | None = None
18+
try:
19+
import zipfile
20+
except ImportError:
21+
pass
22+
2223
try:
2324
from pwd import getpwnam
2425
except ImportError:
@@ -270,7 +271,7 @@ def make_archive(
270271
if base_dir is None:
271272
base_dir = os.curdir
272273

273-
kwargs = {'dry_run': dry_run}
274+
kwargs: dict[str, str | bool | None] = {'dry_run': dry_run}
274275

275276
try:
276277
format_info = ARCHIVE_FORMATS[format]

distutils/cmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def __init__(self, dist: Distribution) -> None:
107107
# timestamps, but methods defined *here* assume that
108108
# 'self.force' exists for all commands. So define it here
109109
# just to be safe.
110-
self.force = None
110+
self.force: bool | None = None
111111

112112
# The 'help' flag is just used for command-line parsing, so
113113
# none of that complicated bureaucracy is needed.

distutils/command/install.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ def initialize_options(self) -> None:
256256
# These select only the installation base; it's up to the user to
257257
# specify the installation scheme (currently, that means supplying
258258
# the --install-{platlib,purelib,scripts,data} options).
259-
self.install_base = None
260-
self.install_platbase = None
259+
self.install_base: str | None = None
260+
self.install_platbase: str | None = None
261261
self.root: str | None = None
262262

263263
# These options are the actual installation directories; if not

distutils/dist.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
Literal,
2424
TypeVar,
2525
Union,
26+
cast,
2627
overload,
2728
)
2829

@@ -849,7 +850,7 @@ def get_command_class(self, command: str) -> type[Command]:
849850
continue
850851

851852
try:
852-
klass = getattr(module, klass_name)
853+
klass = cast("type[Command]", getattr(module, klass_name))
853854
except AttributeError:
854855
raise DistutilsModuleError(
855856
f"invalid command '{command}' (no class '{klass_name}' in module '{module_name}')"

distutils/extension.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ def __init__(
144144

145145
# If there are unknown keyword options, warn about them
146146
if len(kw) > 0:
147-
options = [repr(option) for option in kw]
148-
options = ', '.join(sorted(options))
147+
options = ', '.join(sorted([repr(option) for option in kw]))
149148
msg = f"Unknown Extension options: {options}"
150149
warnings.warn(msg)
151150

distutils/fancy_getopt.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ def getopt(self, args: Sequence[str] | None = None, object=None): # noqa: C901
249249
raise DistutilsArgError(msg)
250250

251251
for opt, val in opts:
252+
value: int | str = val
252253
if len(opt) == 2 and opt[0] == '-': # it's a short option
253254
opt = self.short2long[opt[1]]
254255
else:
@@ -260,21 +261,21 @@ def getopt(self, args: Sequence[str] | None = None, object=None): # noqa: C901
260261
opt = alias
261262

262263
if not self.takes_arg[opt]: # boolean option?
263-
assert val == '', "boolean option can't have value"
264+
assert value == '', "boolean option can't have value"
264265
alias = self.negative_alias.get(opt)
265266
if alias:
266267
opt = alias
267-
val = 0
268+
value = 0
268269
else:
269-
val = 1
270+
value = 1
270271

271272
attr = self.attr_name[opt]
272273
# The only repeating option at the moment is 'verbose'.
273274
# It has a negative option -q quiet, which should set verbose = False.
274-
if val and self.repeat.get(attr) is not None:
275-
val = getattr(object, attr, 0) + 1
276-
setattr(object, attr, val)
277-
self.option_order.append((opt, val))
275+
if value and self.repeat.get(attr) is not None:
276+
value = getattr(object, attr, 0) + 1
277+
setattr(object, attr, value)
278+
self.option_order.append((opt, value))
278279

279280
# for opts
280281
if created_object:

distutils/sysconfig.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,16 @@ def customize_compiler(compiler: CCompiler) -> None:
324324
'AR',
325325
'ARFLAGS',
326326
)
327+
assert isinstance(cc, str)
328+
assert isinstance(cxx, str)
329+
assert isinstance(cflags, str)
330+
assert isinstance(ccshared, str)
331+
assert isinstance(ldshared, str)
332+
assert isinstance(ldcxxshared, str)
333+
assert isinstance(shlib_suffix, str)
334+
assert isinstance(ar_flags, str)
335+
ar = os.environ.get('AR', ar)
336+
assert isinstance(ar, str)
327337

328338
cxxflags = cflags
329339

@@ -354,8 +364,6 @@ def customize_compiler(compiler: CCompiler) -> None:
354364
ldshared = _add_flags(ldshared, 'CPP')
355365
ldcxxshared = _add_flags(ldcxxshared, 'CPP')
356366

357-
ar = os.environ.get('AR', ar)
358-
359367
archiver = ar + ' ' + os.environ.get('ARFLAGS', ar_flags)
360368
cc_cmd = cc + ' ' + cflags
361369
cxx_cmd = cxx + ' ' + cxxflags

distutils/tests/unix_compat.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
from __future__ import annotations
2+
13
import sys
4+
from types import ModuleType
5+
6+
import pytest
27

8+
grp: ModuleType | None = None
9+
pwd: ModuleType | None = None
310
try:
411
import grp
512
import pwd
613
except ImportError:
7-
grp = pwd = None
14+
pass
815

9-
import pytest
1016

1117
UNIX_ID_SUPPORT = grp and pwd
1218
UID_0_SUPPORT = UNIX_ID_SUPPORT and sys.platform != "cygwin"

mypy.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ disable_error_code =
2525
# TODO: Resolve and re-enable these gradually
2626
operator,
2727
arg-type,
28-
assignment,
2928
call-overload,
3029
index,
3130
func-returns-value,

0 commit comments

Comments
 (0)