Skip to content

New Alias System: Add the Alias class #3993

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

Merged
merged 10 commits into from
Jul 29, 2025
Merged

New Alias System: Add the Alias class #3993

merged 10 commits into from
Jul 29, 2025

Conversation

seisman
Copy link
Member

@seisman seisman commented Jul 10, 2025

This PR adds the Alias class, which wraps the _to_string function and is responsible for converting any value into a string or a list of strings.

It's the basis of the new alias system (#4000) and class-like parameters (see #3995). So, please review PRs #3995 and #4000 first.

@seisman seisman added this to the 0.17.0 milestone Jul 10, 2025
@seisman seisman added enhancement Improving an existing feature needs review This PR has higher priority and needs review. and removed needs review This PR has higher priority and needs review. labels Jul 10, 2025
@seisman seisman added the needs review This PR has higher priority and needs review. label Jul 21, 2025
@seisman seisman requested a review from Copilot July 22, 2025 06:08
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces the Alias class as a dataclass-based wrapper for the existing _to_string function to establish the foundation for a new alias system. The class provides a more structured and object-oriented approach to handling parameter aliasing between PyGMT's long-form arguments and GMT's short-form options.

  • Adds the Alias dataclass with attributes matching _to_string parameters
  • Implements a _value property that delegates to the existing _to_string function
  • Provides comprehensive documentation with usage examples
Comments suppressed due to low confidence (1)

pygmt/alias.py:185

  • [nitpick] The property name '_value' uses a leading underscore which typically indicates a private/internal attribute, but this appears to be the main interface for accessing the converted value. Consider using 'value' or 'converted_value' for better API clarity.
    def _value(self) -> str | list[str] | None:

pygmt/alias.py Outdated
@@ -131,3 +132,66 @@ def _to_string(
# "prefix" and "mapping" are ignored. We can enable them when needed.
_value = sequence_join(value, separator=separator, size=size, ndim=ndim, name=name)
return f"{prefix}{_value}"


@dataclasses.dataclass
Copy link
Member Author

Choose a reason for hiding this comment

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

Since it's just a simple class with one property, I'm wondering if we should avoid using dataclass and instead write it like:

class Alias:
    def __init__(value, name=None, prefix="", ....):
        self.value = value
        self.name = name
        self.prefix = prefix
        ...
        self._value = _to_string(
            value=self.value,
            name=self.name,
            prefix=self.prefix,
            mapping=self.mapping,
            separator=self.separator,
            size=self.size,
            ndim=self.ndim,
        )

Copy link
Member Author

Choose a reason for hiding this comment

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

The plain class is about 30% faster than dataclass. Considering that the Alias class is used extensively in the project, I've rewritten it in 4cdef8b.

@seisman seisman added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Jul 29, 2025
@seisman seisman merged commit 3418acd into main Jul 29, 2025
32 of 35 checks passed
@seisman seisman deleted the AliasSystem/alias branch July 29, 2025 03:52
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant