-
Notifications
You must be signed in to change notification settings - Fork 235
New Alias System: Add the AliasSystem class #4000
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
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
654261c
New Alias System: Add the Alias class
seisman 7f43055
Fix
seisman a42c2d8
Merge branch 'main' into AliasSystem/alias
seisman bc0401e
Merge branch 'main' into AliasSystem/alias
seisman fae3ab0
Put name after value
seisman f4f3012
Merge branch 'main' into AliasSystem/alias
seisman cd536bb
New Alias System: Add the AliasSystem class
seisman b0f0d14
Improve the alias system to support single-letter option flags
seisman b244f74
Always pass parameter name
seisman ddcc7b8
Simplify the alias system
seisman 7ff71d1
Fix static tying
seisman c1fdfa5
Improve tests
seisman 571d0dd
Improve comments
seisman d0ebbcb
Merge branch 'main' into AliasSystem/alias
seisman 408199c
Merge branch 'AliasSystem/alias' into AliasSystem/aliassystem
seisman 76f7c6e
Improve error message for recommended long-form parameters
seisman 657f053
Minor fix in tests
seisman 324b973
Simplify the error message handling
seisman a375e69
Improve tests
seisman 99845c2
Implement AliasSystem as a subclass of UserDict
seisman 5259b1e
Simplify the logic of the AliasSystem.merge() method
seisman 92e0a08
Revert changes in basemap.py
seisman 5a9817d
Merge branch 'main' into AliasSystem/alias
seisman 195acfa
Merge branch 'AliasSystem/alias' into AliasSystem/aliassystem
seisman 4cdef8b
Avoid using dataclass
seisman e57954d
No need to store properties that won't be used
seisman 4ce43d1
Merge branch 'AliasSystem/alias' into AliasSystem/aliassystem
seisman 209fda1
Merge branch 'AliasSystem/alias' into AliasSystem/aliassystem
seisman 8d2e1be
Merge branch 'main' into AliasSystem/aliassystem
seisman 7e3fb66
Merge remote-tracking branch 'origin/AliasSystem/aliassystem' into Al…
seisman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| """ | ||
| Tests for the alias system. | ||
| """ | ||
|
|
||
| import pytest | ||
| from pygmt.alias import Alias, AliasSystem | ||
| from pygmt.exceptions import GMTInvalidInput | ||
| from pygmt.helpers import build_arg_list | ||
|
|
||
|
|
||
| def func( | ||
| projection=None, | ||
| region=None, | ||
| frame=None, | ||
| label=None, | ||
| text=None, | ||
| offset=None, | ||
| **kwargs, | ||
| ): | ||
| """ | ||
| A simple function to test the alias system. | ||
| """ | ||
| aliasdict = AliasSystem( | ||
| J=Alias(projection, name="projection"), | ||
| R=Alias(region, name="region", separator="/", size=[4, 6]), | ||
| B=Alias(frame, name="frame"), | ||
| U=[ | ||
| Alias(label, name="label"), | ||
| Alias(text, name="text", prefix="+t"), | ||
| Alias(offset, name="offset", prefix="+o", separator="/"), | ||
| ], | ||
| ).merge(kwargs) | ||
| return build_arg_list(aliasdict) | ||
|
|
||
|
|
||
| def test_alias_system_long_form(): | ||
| """ | ||
| Test that the alias system works with long-form parameters. | ||
| """ | ||
| # One parameter | ||
| assert func(projection="X10c") == ["-JX10c"] | ||
| # Multiple parameters. | ||
| assert func(projection="H10c", region=[0, 10, 0, 20]) == ["-JH10c", "-R0/10/0/20"] | ||
| # Repeatable parameters. | ||
| assert func(frame=["WSen", "xaf", "yaf"]) == ["-BWSen", "-Bxaf", "-Byaf"] | ||
| # Multiple long-form parameters. | ||
| assert func(label="abcd", text="efg", offset=(12, 12)) == ["-Uabcd+tefg+o12/12"] | ||
| assert func( | ||
| projection="H10c", | ||
| region=[0, 10, 0, 20], | ||
| label="abcd", | ||
| text="efg", | ||
| offset=(12, 12), | ||
| frame=["WSen", "xaf", "yaf"], | ||
| ) == ["-BWSen", "-Bxaf", "-Byaf", "-JH10c", "-R0/10/0/20", "-Uabcd+tefg+o12/12"] | ||
|
|
||
|
|
||
| def test_alias_system_one_alias_short_form(): | ||
| """ | ||
| Test that the alias system works when short-form parameters coexist. | ||
| """ | ||
| # Long-form does not exist. | ||
| assert func(A="abc") == ["-Aabc"] | ||
|
|
||
| # Long-form exists but is not given, and short-form is given. | ||
| with pytest.warns( | ||
| SyntaxWarning, | ||
| match="Short-form parameter 'J' is not recommended. Use long-form parameter 'projection' instead.", | ||
| ): | ||
| assert func(J="X10c") == ["-JX10c"] | ||
|
|
||
| # Coexistence of long-form and short-form parameters. | ||
| with pytest.raises( | ||
| GMTInvalidInput, | ||
| match="Short-form parameter 'J' conflicts with long-form parameters and is not recommended. Use long-form parameter 'projection' instead.", | ||
| ): | ||
| func(projection="X10c", J="H10c") | ||
|
|
||
|
|
||
| def test_alias_system_multiple_aliases_short_form(): | ||
| """ | ||
| Test that the alias system works with multiple aliases when short-form parameters | ||
| are used. | ||
| """ | ||
| _msg_long = r"Use long-form parameters 'label', with optional parameters 'text' \(\+t\), 'offset' \(\+o\) instead." | ||
| # Long-form exists but is not given, and short-form is given. | ||
| msg = rf"Short-form parameter 'U' is not recommended. {_msg_long}" | ||
| with pytest.warns(SyntaxWarning, match=msg): | ||
| assert func(U="abcd+tefg") == ["-Uabcd+tefg"] | ||
|
|
||
| # Coexistence of long-form and short-form parameters. | ||
| msg = rf"Short-form parameter 'U' conflicts with long-form parameters and is not recommended. {_msg_long}" | ||
| with pytest.raises(GMTInvalidInput, match=msg): | ||
| func(label="abcd", U="efg") | ||
|
|
||
| with pytest.raises(GMTInvalidInput, match=msg): | ||
| func(text="efg", U="efg") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should't this be overriding the UserDict's
dataattribute? https://docs.python.org/3/library/collections.html#collections.UserDict.dataThen I suppose you can use
updateas method name? Unless it's still not advised to override that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
datais the real dictionary that stores the contents of the UserDict class, so changing the UserDict object also affectsdata.The output is: