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

Add annotations for ModuleType #1525

Merged
merged 2 commits into from
Apr 22, 2022
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
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Release date: TBA

Closes #1512

* Rename ``ModuleSpec`` -> ``module_type`` constructor parameter to match attribute
name and improve typing. Use ``type`` instead.


What's New in astroid 2.11.4?
=============================
Expand Down
65 changes: 27 additions & 38 deletions astroid/interpreter/_import/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt

import abc
import collections
import enum
import importlib
import importlib.machinery
Expand All @@ -13,46 +12,38 @@
import zipimport
from functools import lru_cache
from pathlib import Path
from typing import List, Optional
from typing import List, NamedTuple, Optional, Sequence, Tuple

from . import util

ModuleType = enum.Enum(
"ModuleType",
"C_BUILTIN C_EXTENSION PKG_DIRECTORY "
"PY_CODERESOURCE PY_COMPILED PY_FROZEN PY_RESOURCE "
"PY_SOURCE PY_ZIPMODULE PY_NAMESPACE",
)

class ModuleType(enum.Enum):
"""Python module types used for ModuleSpec."""

_ModuleSpec = collections.namedtuple(
"_ModuleSpec", "name type location " "origin submodule_search_locations"
Copy link
Member

Choose a reason for hiding this comment

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

There was a useless implicit string concatenation here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, could have been to indicate the optional parameter originally, but I'm not certain.

)
C_BUILTIN = enum.auto()
C_EXTENSION = enum.auto()
PKG_DIRECTORY = enum.auto()
PY_CODERESOURCE = enum.auto()
PY_COMPILED = enum.auto()
PY_FROZEN = enum.auto()
PY_RESOURCE = enum.auto()
PY_SOURCE = enum.auto()
PY_ZIPMODULE = enum.auto()
PY_NAMESPACE = enum.auto()


class ModuleSpec(_ModuleSpec):
class ModuleSpec(NamedTuple):
"""Defines a class similar to PEP 420's ModuleSpec

A module spec defines a name of a module, its type, location
and where submodules can be found, if the module is a package.
"""

def __new__(
cls,
name,
module_type,
location=None,
origin=None,
submodule_search_locations=None,
):
return _ModuleSpec.__new__(
cls,
name=name,
type=module_type,
location=location,
origin=origin,
submodule_search_locations=submodule_search_locations,
)
name: str
type: ModuleType
location: "str | None" = None
origin: "str | None" = None
submodule_search_locations: "Sequence[str] | None" = None


class Finder:
Expand Down Expand Up @@ -93,7 +84,7 @@ def contribute_to_path(self, spec, processed):
class ImportlibFinder(Finder):
"""A finder based on the importlib module."""

_SUFFIXES = (
_SUFFIXES: Sequence[Tuple[str, ModuleType]] = (
[(s, ModuleType.C_EXTENSION) for s in importlib.machinery.EXTENSION_SUFFIXES]
+ [(s, ModuleType.PY_SOURCE) for s in importlib.machinery.SOURCE_SUFFIXES]
+ [(s, ModuleType.PY_COMPILED) for s in importlib.machinery.BYTECODE_SUFFIXES]
Expand All @@ -116,13 +107,13 @@ def find_module(
return ModuleSpec(
name=modname,
location=None,
module_type=ModuleType.C_BUILTIN,
type=ModuleType.C_BUILTIN,
)
if spec.loader is importlib.machinery.FrozenImporter:
return ModuleSpec(
name=modname,
location=getattr(spec.loader_state, "filename", None),
module_type=ModuleType.PY_FROZEN,
type=ModuleType.PY_FROZEN,
)
except ValueError:
pass
Expand All @@ -137,15 +128,13 @@ def find_module(
return ModuleSpec(
name=modname,
location=package_directory,
module_type=ModuleType.PKG_DIRECTORY,
type=ModuleType.PKG_DIRECTORY,
)
for suffix, type_ in ImportlibFinder._SUFFIXES:
file_name = modname + suffix
file_path = os.path.join(entry, file_name)
if os.path.isfile(file_path):
return ModuleSpec(
name=modname, location=file_path, module_type=type_
)
return ModuleSpec(name=modname, location=file_path, type=type_)
return None

def contribute_to_path(self, spec, processed):
Expand Down Expand Up @@ -193,7 +182,7 @@ def find_module(self, modname, module_parts, processed, submodule_path):
name=modname,
location="",
origin="namespace",
module_type=ModuleType.PY_NAMESPACE,
type=ModuleType.PY_NAMESPACE,
submodule_search_locations=submodule_path,
)
return None
Expand All @@ -219,7 +208,7 @@ def find_module(self, modname, module_parts, processed, submodule_path):
name=modname,
location=filename,
origin="egg",
module_type=file_type,
type=file_type,
submodule_search_locations=path,
)

Expand All @@ -240,7 +229,7 @@ def find_module(self, modname, module_parts, processed, submodule_path):
name=spec.name,
location=location,
origin=spec.origin,
module_type=module_type,
type=module_type,
submodule_search_locations=list(spec.submodule_search_locations or []),
)
return spec
Expand Down
2 changes: 1 addition & 1 deletion astroid/modutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def file_info_from_modpath(modpath, path=None, context_file=None):
return spec.ModuleSpec(
name="os.path",
location=os.path.__file__,
module_type=spec.ModuleType.PY_SOURCE,
type=spec.ModuleType.PY_SOURCE,
)
return _spec_from_modpath(modpath, path, context)

Expand Down