Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/metadata_dictionary/__init__.py
import collections | import collections | ||||
from types import ModuleType | |||||
from typing import DefaultDict, Set | |||||
import click | import click | ||||
from . import codemeta, maven, npm, python, ruby | from . import codemeta, maven, npm, python, ruby | ||||
MAPPINGS = { | MAPPINGS = { | ||||
"CodemetaMapping": codemeta.CodemetaMapping, | "CodemetaMapping": codemeta.CodemetaMapping, | ||||
"MavenMapping": maven.MavenMapping, | "MavenMapping": maven.MavenMapping, | ||||
"NpmMapping": npm.NpmMapping, | "NpmMapping": npm.NpmMapping, | ||||
"PythonPkginfoMapping": python.PythonPkginfoMapping, | "PythonPkginfoMapping": python.PythonPkginfoMapping, | ||||
"GemspecMapping": ruby.GemspecMapping, | "GemspecMapping": ruby.GemspecMapping, | ||||
} | } | ||||
def list_terms(): | def list_terms() -> DefaultDict[str, Set[ModuleType]]: | ||||
"""Returns a dictionary with all supported CodeMeta terms as keys, | """Returns a dictionary with all supported CodeMeta terms as keys, | ||||
and the mappings that support each of them as values.""" | and the mappings that support each of them as values.""" | ||||
d = collections.defaultdict(set) | d: DefaultDict[str, Set[ModuleType]] = collections.defaultdict(set) | ||||
for mapping in MAPPINGS.values(): | for mapping in MAPPINGS.values(): | ||||
for term in mapping.supported_terms(): | for term in mapping.supported_terms(): # type: ignore | ||||
d[term].add(mapping) | d[term].add(mapping) # type: ignore | ||||
vlorentz: why `# type: ignore`? | |||||
rohan-sachanUnsubmitted Not Done Inline ActionsGetting the following error. It's because the BaseMapping base class in base.py doesn't have supperted_terms() fucntion. Although right now none of the metadata indexers are implementing BaseMapping class directly. One resolution is to just ignore the [attr-defined] and [arg-type] errors on line 23 and 24. If there's another way round, then please let me know. __init__.py:23: error: "Type[BaseMapping]" has no attribute "supported_terms" [attr-defined] __init__.py:24: error: Argument 1 to "add" of "set" has incompatible type "Type[BaseMapping]"; expected Module [arg-type] rohan-sachan: Getting the following error. It's because the BaseMapping base class in base.py doesn't have… | |||||
vlorentzAuthorUnsubmitted Not Done Inline ActionsThe second error is legitimate, values in MAPPINGS are not modules. vlorentz: The second error is legitimate, values in `MAPPINGS` are not modules. | |||||
vlorentzAuthorUnsubmitted Not Done Inline ActionsInstead of using # type:ignore [attr-defined], you should add supported_terms() to the definition of BaseMapping. That way, mypy can actually check types are ok. vlorentz: Instead of using `# type:ignore [attr-defined]`, you should add `supported_terms()` to the… | |||||
return d | return d | ||||
@click.command() | @click.command() | ||||
@click.argument("mapping_name") | @click.argument("mapping_name") | ||||
@click.argument("file_name") | @click.argument("file_name") | ||||
def main(mapping_name, file_name): | def main(mapping_name: str, file_name: str) -> None: | ||||
from pprint import pprint | from pprint import pprint | ||||
with open(file_name, "rb") as fd: | with open(file_name, "rb") as fd: | ||||
file_content = fd.read() | file_content = fd.read() | ||||
res = MAPPINGS[mapping_name]().translate(file_content) | res = MAPPINGS[mapping_name]().translate(file_content) | ||||
pprint(res) | pprint(res) | ||||
if __name__ == "__main__": | if __name__ == "__main__": | ||||
main() | main() |
why # type: ignore?