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

Make default module mapping logic consistent and easy to understand #20513

Merged
merged 4 commits into from
Feb 22, 2024

Conversation

purajit
Copy link
Contributor

@purajit purajit commented Feb 9, 2024

This was motivated by trying to add some additional module and stub-related mappings.

While doing that, I realized the patterns only applied to implementation modules only, and realized that a
refactor would be the easiest way forward, while also making the logic easier to step through.

I haven't added any of those module mappings in this PR since I wanted this to only be the logic change.

Changes

  • Make the replacement functions more generic (allow injecting any separator)
  • Better type hinting - more complete types for generics, also made return values more specific (return values and variables should be hinted as specifically as possible; functions can choose to accept more generic types as arguments)
  • Made impl and type package mappings behave identically
  • The previous logic seemed to have potential for bugs - it does a fallback on each call, has some hackiness around checking for a stubs package, nested conditions, etc. Simplified the module lookup logic. Now it's just:
    • check if it's in the standard module mapping
    • else check if it's in the type stub module mapping
    • else check if the patterns for type stub modules match - this needs to be done first because a stub module might match an implementation module pattern (example, python-pkg-types)
    • else check if the patterns for impl modules match
    • else use the fallback
  • Update tests to ensure symmetry between standard modules and type stub modules
  • Added new test for test_generate_type_stub_mappings_from_pattern_matches_para, which is the main new logic here
  • All other changes created by pants fmt

Testing

  • Added and modified doctest examples, and python src/python/pants/backend/python/dependency_inference/default_module_mapping.py passes
  • pants test src/python/pants/backend/python/dependency_inference/:: passes
  • Ran this build on our monorepo, which uses quite a tangle of every feature here
  • CI

@purajit purajit changed the title 20240207 module mapping logic make default module mapping logic consistent and easy to understand Feb 9, 2024
@purajit purajit changed the title make default module mapping logic consistent and easy to understand [WIP] make default module mapping logic consistent and easy to understand Feb 9, 2024
@purajit purajit force-pushed the 20240207-module-mapping-logic branch from c103685 to 6a1e8ad Compare February 9, 2024 01:57
@purajit purajit force-pushed the 20240207-module-mapping-logic branch from 6a1e8ad to 6d3c78a Compare February 9, 2024 21:42
@purajit purajit force-pushed the 20240207-module-mapping-logic branch from 5f10a36 to 7f60f74 Compare February 9, 2024 21:54
Comment on lines 193 to 200
re.compile(r"""^stubs_(.+)"""): [first_group_hyphen_to_underscore],
re.compile(r"""^types_(.+)"""): [first_group_hyphen_to_underscore],
re.compile(r"""^stubs-(.+)"""): [first_group_hyphen_to_underscore],
re.compile(r"""^types-(.+)"""): [first_group_hyphen_to_underscore],
re.compile(r"""^(.+)_stubs"""): [first_group_hyphen_to_underscore],
re.compile(r"""^(.+)_types"""): [first_group_hyphen_to_underscore],
re.compile(r"""^(.+)-stubs"""): [first_group_hyphen_to_underscore],
re.compile(r"""^(.+)-types"""): [first_group_hyphen_to_underscore],
Copy link
Contributor Author

@purajit purajit Feb 9, 2024

Choose a reason for hiding this comment

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

these do the work of the fallback[6:]/fallback[:6] before, without needing to check for those, or assuming prefix length



@functools.cache
def generate_mappings(proj_name: str, fallback_value: str) -> Iterable[str]:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not needed anymore; essentially, the only thing that should care about the fallback logic should be the main loop. The rest should just deal with their own domains.

@@ -355,23 +349,23 @@ async def map_third_party_modules_to_addresses(
for tgt in all_python_targets.third_party:
resolve = tgt[PythonRequirementResolveField].normalized_value(python_setup)

def add_modules(modules: Iterable[str], *, type_stub: bool = False) -> None:
def add_modules(modules: Iterable[str], *, is_type_stub: bool) -> None:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed default to ensure explicitness of something critical

assert generate_mappings_from_pattern(proj_name, is_type_stub=False) == expected_modules


@pytest.mark.parametrize(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the new test

for pattern, replacements in [
*DEFAULT_MODULE_PATTERN_MAPPING.items(),
*DEFAULT_TYPE_STUB_MODULE_PATTERN_MAPPING.items(),
]:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this and the above add symmetry between the two. From here till the last comment in this file are just pants fmt changes

@purajit purajit changed the title [WIP] make default module mapping logic consistent and easy to understand Make default module mapping logic consistent and easy to understand Feb 9, 2024
@purajit purajit marked this pull request as ready for review February 9, 2024 22:15
re.compile(r"""^stubs[_-](.+)"""): [first_group_hyphen_to_underscore],
re.compile(r"""^types[_-](.+)"""): [first_group_hyphen_to_underscore],
re.compile(r"""^(.+)[_-]stubs"""): [first_group_hyphen_to_underscore],
re.compile(r"""^(.+)[_-]types"""): [first_group_hyphen_to_underscore],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these do the work of fallback[6:]/fallback[:6] before

Copy link
Member

@cognifloyd cognifloyd left a comment

Choose a reason for hiding this comment

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

Looks sane.

@cognifloyd cognifloyd added category:internal CI, fixes for not-yet-released features, etc. backend: Python Python backend-related issues labels Feb 9, 2024
Copy link
Member

@kaos kaos left a comment

Choose a reason for hiding this comment

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

Thanks!

@huonw
Copy link
Contributor

huonw commented Feb 22, 2024

There's a few approvals here, is this waiting on something before merge? (thanks all for contributing and reviewing!)

@kaos
Copy link
Member

kaos commented Feb 22, 2024

Nothing but a little time for us to find if we overlooked something during review, I think. Thanks for bumping.

@kaos kaos merged commit 42ff1dd into pantsbuild:main Feb 22, 2024
24 checks passed
@purajit purajit deleted the 20240207-module-mapping-logic branch March 21, 2024 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend: Python Python backend-related issues category:internal CI, fixes for not-yet-released features, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants