Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assorted MyPy improvments #1663

Merged
merged 5 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/src/hatchling/builders/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class CustomBuilder(Generic[PluginManagerBound]):
PLUGIN_NAME = 'custom'

def __new__( # type: ignore
def __new__( # type: ignore[misc]
cls,
root: str,
plugin_manager: PluginManagerBound | None = None,
Expand Down Expand Up @@ -45,7 +45,7 @@ def __new__( # type: ignore
message = f'Build script does not exist: {build_script}'
raise OSError(message)

hook_class = load_plugin_from_script(path, build_script, BuilderInterface, 'builder') # type: ignore
hook_class = load_plugin_from_script(path, build_script, BuilderInterface, 'builder') # type: ignore[type-abstract]
hook = hook_class(root, plugin_manager=plugin_manager, config=config, metadata=metadata, app=app)

# Always keep the name to avoid confusion
Expand Down
2 changes: 1 addition & 1 deletion backend/src/hatchling/builders/hooks/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class CustomBuildHook:
PLUGIN_NAME = 'custom'

def __new__( # type: ignore
def __new__( # type: ignore[misc]
cls,
root: str,
config: dict[str, Any],
Expand Down
2 changes: 1 addition & 1 deletion backend/src/hatchling/builders/hooks/plugin/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@

@hookimpl
def hatch_register_build_hook() -> list[type[BuildHookInterface]]:
return [CustomBuildHook, VersionBuildHook] # type: ignore
return [CustomBuildHook, VersionBuildHook] # type: ignore[list-item]
2 changes: 1 addition & 1 deletion backend/src/hatchling/builders/plugin/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@

@hookimpl
def hatch_register_builder() -> list[type[BuilderInterface]]:
return [AppBuilder, BinaryBuilder, CustomBuilder, SdistBuilder, WheelBuilder] # type: ignore
return [AppBuilder, BinaryBuilder, CustomBuilder, SdistBuilder, WheelBuilder] # type: ignore[list-item]
4 changes: 2 additions & 2 deletions backend/src/hatchling/cli/dep/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def dependencies_in_sync(
if sys_path is None:
sys_path = sys.path
if environment is None:
environment = default_environment() # type: ignore
environment = default_environment() # type: ignore[assignment]

installed_distributions = DistributionCache(sys_path)
return all(dependency_in_sync(requirement, environment, installed_distributions) for requirement in requirements) # type: ignore
return all(dependency_in_sync(requirement, environment, installed_distributions) for requirement in requirements) # type: ignore[arg-type]
Copy link
Contributor Author

@DimitriPapadopoulos DimitriPapadopoulos Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This a real issue:

  • dependencies_in_sync/dependency_in_sync require environment to be a dict[str, str]:
    def dependencies_in_sync(
    requirements: list[Requirement], sys_path: list[str] | None = None, environment: dict[str, str] | None = None
  • packaging.markers.default_environment returns an Environment which is a TypedDict

Not sure how to best handle this. Change dependencies_in_sync/dependency_in_sync to expect Environment or TypedDict? Cast the result from default_environment to a dict[str, str]?

4 changes: 2 additions & 2 deletions backend/src/hatchling/metadata/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class CustomMetadataHook:
PLUGIN_NAME = 'custom'

def __new__( # type: ignore
def __new__( # type: ignore[misc]
cls,
root: str,
config: dict[str, Any],
Expand All @@ -32,7 +32,7 @@ def __new__( # type: ignore
message = f'Build script does not exist: {build_script}'
raise OSError(message)

hook_class = load_plugin_from_script(path, build_script, MetadataHookInterface, 'metadata_hook') # type: ignore
hook_class = load_plugin_from_script(path, build_script, MetadataHookInterface, 'metadata_hook') # type: ignore[type-abstract]
hook = hook_class(root, config, *args, **kwargs)

# Always keep the name to avoid confusion
Expand Down
2 changes: 1 addition & 1 deletion backend/src/hatchling/metadata/plugin/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@

@hookimpl
def hatch_register_metadata_hook() -> type[MetadataHookInterface]:
return CustomMetadataHook # type: ignore
return CustomMetadataHook # type: ignore[return-value]
2 changes: 1 addition & 1 deletion backend/src/hatchling/metadata/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def project_metadata_from_core_metadata(core_metadata: str) -> dict[str, Any]:
header_registry = HeaderRegistry()

message = email.message_from_string(core_metadata)
metadata = {}
metadata: dict[str, Any] = {}

if name := message.get('Name'):
metadata['name'] = name
Expand Down
4 changes: 2 additions & 2 deletions backend/src/hatchling/plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def load_plugin_from_script(path: str, script_name: str, plugin_class: type[T],
from importlib.util import module_from_spec, spec_from_file_location

spec = spec_from_file_location(script_name, path)
module = module_from_spec(spec) # type: ignore
spec.loader.exec_module(module) # type: ignore
module = module_from_spec(spec) # type: ignore[arg-type]
spec.loader.exec_module(module) # type: ignore[union-attr]

plugin_finder = f'get_{plugin_id}'
names = dir(module)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/hatchling/version/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def read(self, *, pattern: str | bool) -> str:
return self.__cached_read_data[0]

def set_version(self, version: str) -> None:
_old_version, file_contents, (start, end) = self.__cached_read_data # type: ignore
_old_version, file_contents, (start, end) = self.__cached_read_data # type: ignore[misc]
with open(self.__path, 'w', encoding='utf-8') as f:
f.write(f'{file_contents[:start]}{version}{file_contents[end:]}')

Expand Down
4 changes: 2 additions & 2 deletions backend/src/hatchling/version/source/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ def get_version_data(self) -> dict:
absolute_search_paths.append(os.path.normpath(os.path.join(self.root, search_path)))

spec = spec_from_file_location(os.path.splitext(path)[0], path)
module = module_from_spec(spec) # type: ignore
module = module_from_spec(spec) # type: ignore[arg-type]

old_search_paths = list(sys.path)
try:
sys.path[:] = [*absolute_search_paths, *old_search_paths]
spec.loader.exec_module(module) # type: ignore
spec.loader.exec_module(module) # type: ignore[union-attr]
finally:
sys.path[:] = old_search_paths

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ exclude = [
[tool.mypy]
disallow_untyped_defs = false
disallow_incomplete_defs = false
enable_error_code = ["ignore-without-code", "truthy-bool"]
follow_imports = "normal"
ignore_missing_imports = true
pretty = true
show_column_numbers = true
show_error_codes = true
warn_no_return = false
warn_unused_ignores = true

Expand Down
8 changes: 4 additions & 4 deletions src/hatch/cli/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def __init__(
is_interactive: bool,
verbosity: int,
spinner_style: str,
waiting_style: Style,
success_style: Style,
waiting_style: Style | str,
success_style: Style | str,
initializer: Callable,
finalizer: Callable,
):
Expand Down Expand Up @@ -331,8 +331,8 @@ def status(self) -> BorrowedStatus:
is_interactive=self.console.is_interactive,
verbosity=self.verbosity,
spinner_style=self._style_spinner,
waiting_style=self._style_level_waiting, # type: ignore[arg-type]
success_style=self._style_level_success, # type: ignore[arg-type]
waiting_style=self._style_level_waiting,
success_style=self._style_level_success,
initializer=lambda: setattr(self.platform, 'displaying_status', True), # type: ignore[attr-defined]
finalizer=lambda: setattr(self.platform, 'displaying_status', False), # type: ignore[attr-defined]
)
Expand Down
4 changes: 2 additions & 2 deletions src/hatch/dep/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def dependencies_in_sync(
if sys_path is None:
sys_path = sys.path
if environment is None:
environment = default_environment() # type: ignore
environment = default_environment() # type: ignore[assignment]

installed_distributions = DistributionCache(sys_path)
return all(dependency_in_sync(requirement, environment, installed_distributions) for requirement in requirements) # type: ignore
return all(dependency_in_sync(requirement, environment, installed_distributions) for requirement in requirements) # type: ignore[arg-type]
2 changes: 1 addition & 1 deletion src/hatch/env/collectors/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class CustomEnvironmentCollector:
PLUGIN_NAME = 'custom'

def __new__( # type: ignore
def __new__( # type: ignore[misc]
cls,
root: str,
config: dict[str, Any],
Expand Down
2 changes: 1 addition & 1 deletion src/hatch/env/collectors/plugin/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@

@hookimpl
def hatch_register_environment_collector() -> list[type[EnvironmentCollectorInterface]]:
return [CustomEnvironmentCollector, DefaultEnvironmentCollector] # type: ignore
return [CustomEnvironmentCollector, DefaultEnvironmentCollector] # type: ignore[list-item]
4 changes: 2 additions & 2 deletions src/hatch/plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def load_plugin_from_script(
from importlib.util import module_from_spec, spec_from_file_location

spec = spec_from_file_location(script_name, path)
module = module_from_spec(spec) # type: ignore
spec.loader.exec_module(module) # type: ignore
module = module_from_spec(spec) # type: ignore[arg-type]
spec.loader.exec_module(module) # type: ignore[union-attr]

plugin_finder = f'get_{plugin_id}'
names = dir(module)
Expand Down
2 changes: 1 addition & 1 deletion src/hatch/utils/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def populate_default_popen_kwargs(self, kwargs: dict[str, Any], *, shell: bool)
@staticmethod
def stream_process_output(process: Popen) -> Iterable[str]:
# To avoid blocking never use a pipe's file descriptor iterator. See https://bugs.python.org/issue3907
for line in iter(process.stdout.readline, b''): # type: ignore
for line in iter(process.stdout.readline, b''): # type: ignore[union-attr]
yield line.decode('utf-8')

@property
Expand Down
Loading