cool-easter-32542
08/26/2023, 12:11 AMDEFAULT_MODULE_MAPPING. And it is super annoying if every project that uses pants has make the same changes.
Describe the solution you'd like
Ability to define a pattern glob(regex) that can do the mapping.
So, here is a proposal that has two pieces. The first is a "hard coded" solution for google.cloud.*. The second is a potential way to implement this in a more generic fashion.
google.cloud.*
diff --git a/src/python/pants/backend/python/dependency_inference/module_mapper.py b/src/python/pants/backend/python/dependency_inference/module_mapper.py
index a7e65922d0..dc98e81fd1 100644
--- a/src/python/pants/backend/python/dependency_inference/module_mapper.py
+++ b/src/python/pants/backend/python/dependency_inference/module_mapper.py
@@ -12,6 +12,7 @@ from dataclasses import dataclass
from functools import total_ordering
from pathlib import PurePath
from typing import DefaultDict, Iterable, Mapping, Tuple
+import re
from packaging.utils import canonicalize_name as canonicalize_project_name
@@ -40,6 +41,8 @@ from pants.util.strutil import softwrap
logger = logging.getLogger(__name__)
+GOOGLE_CLOUD_REGEX = re.compile(r"^google-cloud-(?P<service>.*)")
+
ResolveName = str
@@ -347,6 +350,11 @@ async def map_third_party_modules_to_addresses(
proj_name = canonicalize_project_name(req.project_name)
fallback_value = req.project_name.strip().lower().replace("-", "_")
+ matches = GOOGLE_CLOUD_REGEX.match(proj_name)
+ if matches is not None:
+ service = matches.group("service")
+ fallback_value = f"google.cloud.{service}"
+
in_stubs_map = proj_name in DEFAULT_TYPE_STUB_MODULE_MAPPING
starts_with_prefix = fallback_value.startswith(("types_", "stubs_"))
ends_with_prefix = fallback_value.endswith(("_types", "_stubs"))
More generic
To make this more generic, maybe a map similar to DEFAULT_MODULE_MAPPING but one that takes a regex pattern and match pattern e.g.:
DEFAULT_MODULE_GLOB_MAPPING = {
"^google-cloud-(.*)": ("google.cloud.$1", "google.cloud.$1_v1")),
}
Describe alternatives you've considered
Alternative is to map them in a BUILD and/or continue adding to DEFAULT_MODULE_MAPPING.
Additional context
NA
pantsbuild/pantscool-easter-32542
09/25/2023, 11:51 AM