pants icon indicating copy to clipboard operation
pants copied to clipboard

Make default module mapping logic consistent and easy to understand

Open purajit opened this issue 6 months ago • 0 comments

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 avatar Feb 09 '24 00:02 purajit