Skip to content

Commit

Permalink
Type all get/set dunders (#4584)
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri authored Aug 27, 2024
2 parents 2d63f5c + 2e2e918 commit 2e5958b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 9 deletions.
14 changes: 11 additions & 3 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,15 @@ def _added_new(self, dist):
for callback in self.callbacks:
callback(dist)

def __getstate__(self):
def __getstate__(
self,
) -> tuple[
list[str],
dict[str | None, list[str]],
dict[str, Distribution],
dict[str, str],
list[Callable[[Distribution], object]],
]:
return (
self.entries[:],
self.entry_keys.copy(),
Expand All @@ -1103,7 +1111,7 @@ def __getstate__(self):
self.callbacks[:],
)

def __setstate__(self, e_k_b_n_c):
def __setstate__(self, e_k_b_n_c) -> None:
entries, keys, by_key, normalized_to_canonical_keys, callbacks = e_k_b_n_c
self.entries = entries[:]
self.entry_keys = keys.copy()
Expand Down Expand Up @@ -3171,7 +3179,7 @@ def __str__(self) -> str:
version = version or "[unknown version]"
return "%s %s" % (self.project_name, version)

def __getattr__(self, attr):
def __getattr__(self, attr: str):
"""Delegate all unrecognized public attributes to .metadata provider"""
if attr.startswith('_'):
raise AttributeError(attr)
Expand Down
2 changes: 1 addition & 1 deletion setuptools/command/build_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def run(self):
# output files are.
self.byte_compile(orig.build_py.get_outputs(self, include_bytecode=False))

def __getattr__(self, attr):
def __getattr__(self, attr: str):
"lazily compute data files"
if attr == 'data_files':
self.data_files = self._get_data_files()
Expand Down
2 changes: 1 addition & 1 deletion setuptools/command/develop.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class VersionlessRequirement:
def __init__(self, dist):
self.__dist = dist

def __getattr__(self, name):
def __getattr__(self, name: str):
return getattr(self.__dist, name)

def as_requirement(self):
Expand Down
5 changes: 4 additions & 1 deletion setuptools/command/test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import annotations

from setuptools import Command
from setuptools.warnings import SetuptoolsDeprecationWarning


def __getattr__(name):
# Would restrict to Literal["test"], but mypy doesn't support it: https://github.com/python/mypy/issues/8203
def __getattr__(name: str) -> type[_test]:
if name == 'test':
SetuptoolsDeprecationWarning.emit(
"The test command is disabled and references to it are deprecated.",
Expand Down
2 changes: 1 addition & 1 deletion setuptools/config/expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _find_assignments(self) -> Iterator[tuple[ast.AST, ast.AST]]:
elif isinstance(statement, ast.AnnAssign) and statement.value:
yield (statement.target, statement.value)

def __getattr__(self, attr):
def __getattr__(self, attr: str):
"""Attempt to load an attribute "statically", via :func:`ast.literal_eval`."""
try:
return next(
Expand Down
2 changes: 1 addition & 1 deletion setuptools/config/setupcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def parsers(self):
'%s must provide .parsers property' % self.__class__.__name__
)

def __setitem__(self, option_name, value):
def __setitem__(self, option_name, value) -> None:
target_obj = self.target_obj

# Translate alias into real name.
Expand Down
3 changes: 2 additions & 1 deletion setuptools/tests/test_build_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import tarfile
from concurrent import futures
from pathlib import Path
from typing import Any, Callable
from zipfile import ZipFile

import pytest
Expand Down Expand Up @@ -44,7 +45,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.pool = futures.ProcessPoolExecutor(max_workers=1)

def __getattr__(self, name):
def __getattr__(self, name: str) -> Callable[..., Any]:
"""Handles arbitrary function invocations on the build backend."""

def method(*args, **kw):
Expand Down

0 comments on commit 2e5958b

Please sign in to comment.