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

rewrite to datetime.UTC #764

Merged
merged 1 commit into from Dec 2, 2022
Merged

rewrite to datetime.UTC #764

merged 1 commit into from Dec 2, 2022

Conversation

rominf
Copy link
Contributor

@rominf rominf commented Dec 2, 2022

Rewrite:

import datetime

datetime.timezone.utc

to alias:

import datetime

datetime.UTC

in Python >= 3.11.

Fixes: #755

Comment on lines 27 to 28
isinstance(parent, ast.Attribute) and
parent.attr == 'utc'
Copy link
Owner

Choose a reason for hiding this comment

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

parent should be avoided when possible -- this should start from the datetime Attribute

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've just pushed a new version without parent. Did I get it right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

datetime is not an attribute, but a Name.

Comment on lines 9 to 41
@pytest.mark.parametrize(
('s', 'expected'),
(
pytest.param(
'import datetime\n'
'print(datetime.timezone.utc)',

'import datetime\n'
'print(datetime.UTC)',

id='rewriting to alias',
),
pytest.param(
'import datetime\n'
'print(datetime.timezone(-1))',

'import datetime\n'
'print(datetime.timezone(-1))',

id='not rewriting timezone object to alias',
),
),
)
def test_fix_datetime_utc_alias(s, expected):
assert _fix_plugins(s, settings=Settings(min_version=(3,))) == s
assert _fix_plugins(s, settings=Settings(min_version=(3, 11))) == expected
Copy link
Owner

Choose a reason for hiding this comment

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

there should be clearly separate noop and non-noop tests -- please follow one of the other tests as an example

Rewrite:
```python
import datetime

datetime.timezone.utc
```
to alias:
```python
import datetime

datetime.UTC
```
in Python >= 3.11.

Fixes: #755
Copy link
Owner

@asottile asottile left a comment

Choose a reason for hiding this comment

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

close enough

@asottile asottile merged commit 3879369 into asottile:main Dec 2, 2022
@rominf rominf deleted the rominf/datetime-utc-alias branch December 12, 2022 05:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Plugin idea: Add 3.11 library conversion for UTC timezone
2 participants