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

Fixes looking around #281

Closed
wants to merge 12 commits into from
Closed

Conversation

fstagni
Copy link
Contributor

@fstagni fstagni commented Aug 21, 2024

No description provided.

@@ -569,7 +569,7 @@ def test_set_job_status_offset_naive_datetime_return_bad_request(
valid_job_id: int,
):
# Act
date = datetime.utcnow().isoformat(sep=" ") # noqa: DTZ003
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this was on purpose, given that there was the proper noqa https://docs.astral.sh/ruff/rules/call-datetime-utcnow/
I would guess it has to do with DIRAC compatibility but I don't remember

Copy link
Contributor Author

Choose a reason for hiding this comment

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

utcnow will be deprecated. What I put is of course equivalent.

Copy link
Contributor

Choose a reason for hiding this comment

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

Even though it is subtle, it is not

In [19]: tz_now = datetime.now(tz=timezone.utc); now = datetime.utcnow()

In [20]: tz_now
Out[20]: datetime.datetime(2024, 8, 22, 21, 13, 40, 232387, tzinfo=datetime.timezone.utc)

In [21]: now
Out[21]: datetime.datetime(2024, 8, 22, 21, 13, 40, 232433)

In [22]: tz_now - now
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[22], line 1
----> 1 tz_now - now

TypeError: can't subtract offset-naive and offset-aware datetimes

I was tricked by that in DIRAC in the past:

https://github.com/DIRACGrid/DIRAC/blob/f18be6f9d27f5055512f2c372718734270a060ae/src/DIRAC/Core/Security/m2crypto/asn1_utils.py#L196-L201

But indeed here we should check why was this done and maybe your fix is good.

Copy link
Contributor Author

@fstagni fstagni Aug 23, 2024

Choose a reason for hiding this comment

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

Further converting it is OK:

In [9]: formatted__tz_now = tz_now.isoformat(sep=' ').split("+")[0]

In [10]: formatted__now = now.isoformat(sep=' ').split("+")[0]

In [14]: formatted__tz_now
Out[14]: '2024-08-23 08:56:13.234331'

In [15]: formatted__now
Out[15]: '2024-08-23 08:56:13.234372'

In [16]: date_obj__tz_now = datetime.strptime(formatted__tz_now, '%Y-%m-%d %H:%M:%S.%f')

In [17]: date_obj__now = datetime.strptime(formatted__now, '%Y-%m-%d %H:%M:%S.%f')

In [18]: date_obj__tz_now -  date_obj__now
Out[18]: datetime.timedelta(days=-1, seconds=86399, microseconds=999959)

But:

In [20]: date_obj__tz_now__no_split = datetime.strptime(formatted__tz_now__no_split, '%Y-%m-%d %H:%M:%S.%f')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[20], line 1
----> 1 date_obj__tz_now__no_split = datetime.strptime(formatted__tz_now__no_split, '%Y-%m-%d %H:%M:%S.%f')

File ~/miniforge3/envs/dirac-development/lib/python3.11/_strptime.py:567, in _strptime_datetime(cls, data_string, format)
    564 def _strptime_datetime(cls, data_string, format="%a %b %d %H:%M:%S %Y"):
    565     """Return a class cls instance based on the input string and the
    566     format string."""
--> 567     tt, fraction, gmtoff_fraction = _strptime(data_string, format)
    568     tzname, gmtoff = tt[-2:]
    569     args = tt[:6] + (fraction,)

File ~/miniforge3/envs/dirac-development/lib/python3.11/_strptime.py:352, in _strptime(data_string, format)
    349     raise ValueError("time data %r does not match format %r" %
    350                      (data_string, format))
    351 if len(data_string) != found.end():
--> 352     raise ValueError("unconverted data remains: %s" %
    353                       data_string[found.end():])
    355 iso_year = year = None
    356 month = day = 1

ValueError: unconverted data remains: +00:00

In [21]: formatted__tz_now__no_split
Out[21]: '2024-08-23 08:56:13.234331+00:00'

In [22]: date_obj__tz_now__no_split = datetime.strptime(formatted__tz_now__no_split, '%Y-%m-%d %H:%M:%S.%f+00:00')

In [23]:  date_obj__tz_now__no_split
Out[23]: datetime.datetime(2024, 8, 23, 8, 56, 13, 234331)

In [24]: date_obj__tz_now__no_split -  date_obj__now
Out[24]: datetime.timedelta(days=-1, seconds=86399, microseconds=999959)

...I guess it is a feature, but to me it even feels like a python bug! Bof!

.github/workflows/main.yml Outdated Show resolved Hide resolved
@fstagni fstagni force-pushed the fixes_looking_around branch 5 times, most recently from dfec561 to 5b30fd0 Compare August 22, 2024 08:29
@chaen
Copy link
Contributor

chaen commented Aug 23, 2024

Why removing the __future__ import ?

@fstagni
Copy link
Contributor Author

fstagni commented Aug 23, 2024

Why removing the __future__ import ?

Because since python 3.7 is only necessary for specific cases.

@chrisburr
Copy link
Member

chrisburr commented Aug 23, 2024

Because since python 3.7 is only necessary for specific cases.

It was added in 3.7 so that can't be the case 😉

The intention is to make the from __future__ import annotations the default (see PEP-563 but it keeps getting deffered.

@chaen
Copy link
Contributor

chaen commented Aug 23, 2024

It actually should be everywhere except specific places like

# FastAPI bug:
# We normally would use `from __future__ import annotations`
# but a bug in FastAPI prevents us from doing so
# https://github.com/tiangolo/fastapi/pull/11355
# Until it is merged, we can work around it by using strings.

@fstagni
Copy link
Contributor Author

fstagni commented Aug 23, 2024

Sorry, I meant 3.10, but indeed I realise now that the decision to make it mandatory was postponed and it is not even in 3.12. So, I revert this change.

@fstagni fstagni force-pushed the fixes_looking_around branch from 5b30fd0 to 60f58b2 Compare August 23, 2024 11:52
@@ -15,8 +15,7 @@ def compare_keys(key1, key2):
def test_token_signing_key(tmp_path):
private_key = rsa.generate_private_key(
public_exponent=65537,
# DANGER: 512-bits is a bad idea for prod but makes the test notably faster!
key_size=512,
key_size=1024,
Copy link
Member

Choose a reason for hiding this comment

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

Can we avoid making the tests slower by using elliptic curves?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this PR? Can we leave for another one?

python-gfal2
mypy
pip
environment-file: environment.yml
Copy link
Contributor

Choose a reason for hiding this comment

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

it's not this environment.yml but the one from container-images that should be used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This environment.yml defines the environment where all of us are working locally, right? So, it's the natural environment where all unit tests run.

In container-images there are many environment.yml files. What is here is, permit me that, the superset of all of them.

Copy link
Contributor

Choose a reason for hiding this comment

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

absolutely, but this is what we want to try to avoid. We want to have the minimal set of dependencies installed to keep control of what we actually need, and this is why we did not blindly put the environment.yml before

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, you want to repeat, for ONLY the unit tests run by the CI, the ~same environment (because it is not going to be very different) that we all have in our local machines, and that we all use for running the unit tests locally? Meaning that I can run the unit tests locally, they all pass, then create the PR only to discover that there they are failing. If there's a version to restrict (like for the one for typer), that, like it or not, we have to do it from time to time, then we would need to apply that fix in even one more place.

There is already a noted duplication between setup.cfg and environment.yml and I do not think we should have even more.

The environment where the unit tests run is already NOT what it is run in production. Which is fair and fine. But that is why the integrations tests are there.

Copy link
Member

Choose a reason for hiding this comment

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

We expect the CI to run in an environment which is different to out local machines as we intetionally only install the bare minimum in the CI. We also let it use the newest possible version to try and catch issues before they affect the harder/slower to debug tests.

Maybe we want to drop the environment.yaml file entirely but that's a separate discussion to start and maybe we want to use something else to harmonise this but we've not got around to trying it.

@fstagni fstagni force-pushed the fixes_looking_around branch 3 times, most recently from 03480d7 to 992c14e Compare August 26, 2024 11:54
@fstagni fstagni marked this pull request as ready for review August 26, 2024 12:18
@fstagni fstagni force-pushed the fixes_looking_around branch from 992c14e to f526a06 Compare August 28, 2024 12:13
@fstagni fstagni force-pushed the fixes_looking_around branch from f526a06 to 129a37f Compare August 28, 2024 12:18
@@ -36,3 +36,10 @@ repos:
- types-aiobotocore[essential]
- boto3-stubs[essential]
exclude: ^(diracx-client/src/diracx/client/|diracx-[a-z]+/tests/|diracx-testing/|build)

- repo: https://github.com/asottile/pyupgrade
Copy link
Member

Choose a reason for hiding this comment

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

We have discussed this before...

Copy link
Member

Choose a reason for hiding this comment

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

@fstagni
Copy link
Contributor Author

fstagni commented Aug 28, 2024

Superseded by #286

@fstagni fstagni closed this Aug 28, 2024
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.

3 participants