Skip to content

clib.session: Refactor to simplify the checking of currently open session - #3523

Merged
seisman merged 3 commits into
mainfrom
refactor/session-pointer
Oct 16, 2024
Merged

clib.session: Refactor to simplify the checking of currently open session#3523
seisman merged 3 commits into
mainfrom
refactor/session-pointer

Conversation

@seisman

Copy link
Copy Markdown
Member

Just some refactoring to simplify the codes. See the changes for more details.

@seismanseisman added maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog needs review This PR has higher priority and needs review. labels Oct 16, 2024
@seismanseisman added this to the 0.14.0 milestone Oct 16, 2024
Comment threadpygmt/clib/session.py
the context manager).
"""
if not hasattr(self, "_session_pointer") or self._session_pointer is None:
if getattr(self, "_session_pointer", None) is None:

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

if not hasattr(self, "_session_pointer") or self._session_pointer is None: is equivalent to if getattr(self, "_session_pointer", None) is None:.

Comment threadpygmt/clib/session.py
@@ -338,19 +338,12 @@ def create(self, name: str):
name
A name for this session. Doesn't really affect the outcome.
"""

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

If self.session_pointer is defined and is not None, then we know we have an open session. The try..except statement was added in 2018 (#210) and is not necessary.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As mentioned in #3448 (comment), a with contextlib.supress block should ideally be used when there is only one line below it. I feel like we should keep the previous try-except block.

@seismanseismanOct 16, 2024

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

It's impossible that the msg assignment and the raise GMTCLibError lines can raise a GMTCLibNoSessionError exception, so we should be safe.

Edit: Read astral-sh/ruff#1947 again, actually it's unclear if the raise GMTCLibError statement will be executed or not. I think we have to go back to try..except.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Reverted back to try..except in da9c5e1 and improve the comments to make it easier to understand.

@seismanseisman removed the needs review This PR has higher priority and needs review. label Oct 16, 2024
@seisman
seisman marked this pull request as draft October 16, 2024 04:41
@seisman
seismanforce-pushed the refactor/session-pointer branch from b960654 to 42ae467CompareOctober 16, 2024 04:49
@seisman
seismanforce-pushed the refactor/session-pointer branch from 42ae467 to 3d0c91bCompareOctober 16, 2024 04:54
@seisman
seisman marked this pull request as ready for review October 16, 2024 04:54
@seismanseisman added the needs review This PR has higher priority and needs review. label Oct 16, 2024
@seismanseisman 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 Oct 16, 2024
Comment threadpygmt/clib/session.py Outdated
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
@seismanseisman removed the final review call This PR requires final review and approval from a second reviewer label Oct 16, 2024
@seisman
seisman merged commit c0ad049 into mainOct 16, 2024
@seisman
seisman deleted the refactor/session-pointer branch October 16, 2024 23:10
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenanceBoring but important stuff for the core devsskip-changelogSkip adding Pull Request to changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@seisman@michaelgrund@weiji14