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

Add utility function to strip '.py' extensions #3658

Merged
merged 5 commits into from Jan 24, 2022
Merged

Conversation

mguaypaq
Copy link
Member

@mguaypaq mguaypaq commented Jan 20, 2022

Checklist

GitHub

PR contents

Description

The previous use of str.strip(".py") is a bug waiting to happen,
because it strips from both sides of the string (instead of just
the end), and more importantly, treats ".py" as a set of
characters rather than a substring to look for (see the docs):

$ python3
>>> "ppp.yyy".strip(".py")
''
>>> "no_dot_py".strip(".py")
'no_dot_'
>>> ".py_prefix".strip(".py")
'_prefix'

All uses of .strip(".py") are either in utils/sys.py itself or import this module, so it seemed like the natural place to add this utility function.

Linked issues

The previous use of str.strip(".py") is a bug waiting to happen,
because it strips from both sides of the string (instead of just
the end), and more importantly, treats ".py" as a set of
characters rather than a substring to look for:

$ python3
>>> "ppp.yyy".strip(".py")
''
>>> "no_dot_py".strip(".py")
'no_dot_'
>>> ".py_prefix".strip(".py")
'_prefix'
@mguaypaq mguaypaq added bug category: fixes an error in the code SCT API: utils.py context: labels Jan 20, 2022
@mguaypaq mguaypaq added this to the 5.5 milestone Jan 20, 2022
@mguaypaq mguaypaq self-assigned this Jan 20, 2022
@codecov
Copy link

codecov bot commented Jan 20, 2022

Codecov Report

Merging #3658 (396f628) into master (5a133f4) will decrease coverage by 0.00%.
The diff coverage is 75.00%.

Flag Coverage Δ
api-tests 24.18% <50.00%> (+<0.01%) ⬆️
cli-tests 56.64% <75.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
spinalcordtoolbox/utils/sys.py 60.19% <60.00%> (-0.14%) ⬇️
...alcordtoolbox/scripts/sct_straighten_spinalcord.py 57.60% <100.00%> (ø)
spinalcordtoolbox/utils/shell.py 69.74% <100.00%> (ø)

Copy link
Member

@joshuacwnewton joshuacwnewton left a comment

Choose a reason for hiding this comment

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

I think your concerns are valid! Just as an alternate suggestion, though, how do you feel about using str.removesuffix instead? It's also mentioned in the docs for str.rstrip.

That way there's one fewer utility function we have to maintain in our codebase.

EDIT: I missed the "New in version 3.9". Oops! We can't use the feature that I suggested, since we're on Python 3.7 atm.

spinalcordtoolbox/utils/sys.py Outdated Show resolved Hide resolved
testing/api/test_utils.py Outdated Show resolved Hide resolved
Until we upgrade to Python 3.9, which has this string method
as a builtin, we can use the code from PEP 616.
Copy link
Member

@joshuacwnewton joshuacwnewton left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you for this change. :)

@joshuacwnewton joshuacwnewton merged commit 4010cad into master Jan 24, 2022
@joshuacwnewton joshuacwnewton deleted the mgp/strip_py branch January 24, 2022 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug category: fixes an error in the code SCT API: utils.py context:
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants