Skip to content

DeprecationWarning('datetime.utcnow() is deprecated #175

@jdkloe

Description

@jdkloe

In preparation for Fedora 39 we have been building and testing python modules for the upcoming python3.12 version (tested with Python 3.12.0b3).
For python-metar this resulted in 2 test failures:

# pytest-3
================================== test session starts ===================================
platform linux -- Python 3.12.0b3, pytest-7.3.2, pluggy-1.0.0
rootdir: /builddir/build/BUILD/metar-1.10.0
configfile: setup.cfg
collected 70 items                                                                       

test/test_direction.py ...                                                         [  4%]
test/test_distance.py ....                                                         [ 10%]
test/test_metar.py ..................................F........F.....               [ 80%]
test/test_precipitation.py .                                                       [ 81%]
test/test_pressure.py ....                                                         [ 87%]
test/test_speed.py ....                                                            [ 92%]
test/test_station.py .                                                             [ 94%]
test/test_temperature.py ....                                                      [100%]

======================================== FAILURES ========================================
__________________________________ test_issue51_strict ___________________________________

    def test_issue51_strict():
        """Check that setting strict=False prevents a ParserError"""
        with warnings.catch_warnings(record=True) as w:
            report = Metar.Metar(sta_time + "90010KT", strict=False)
>       assert len(w) == 1
E       assert 2 == 1
E        +  where 2 = len([<warnings.WarningMessage object at 0x7f74e47e87a0>, <warnings.WarningMessage object at 0x7f74e47e8890>])

test/test_metar.py:401: AssertionError
__________________________________ test_not_strict_mode __________________________________

    def test_not_strict_mode():
        """Test the strict attribute on parsing."""
        # This example metar has an extraneous 'M' in it, but the rest is fine
        # Let's make sure that we can activate a non-strict mode, and flag that
        # there are unparsed portions
        code = "K9L2 100958Z AUTO 33006KT 10SM CLR M A3007 RMK AO2 SLPNO FZRANO $"
        raisesParserError(code)
    
        with warnings.catch_warnings(record=True) as w:
            report = Metar.Metar(code, strict=False)
>       assert len(w) == 1
E       assert 2 == 1
E        +  where 2 = len([<warnings.WarningMessage object at 0x7f74e3e21580>, <warnings.WarningMessage object at 0x7f74e3e20b30>])

test/test_metar.py:638: AssertionError
==================================== warnings summary ====================================
../../../../usr/lib/python3.12/site-packages/_pytest/config/__init__.py:1314
  /usr/lib/python3.12/site-packages/_pytest/config/__init__.py:1314: PytestConfigWarning: Unknown config option: pep8ignore
  
    self._warn_or_fail_if_strict(f"Unknown config option: {key}\n")

../../../../usr/lib/python3.12/site-packages/_pytest/config/__init__.py:1314
  /usr/lib/python3.12/site-packages/_pytest/config/__init__.py:1314: PytestConfigWarning: Unknown config option: pep8maxlinelength
  
    self._warn_or_fail_if_strict(f"Unknown config option: {key}\n")

test/test_metar.py:14
  /builddir/build/BUILD/metar-1.10.0/test/test_metar.py:14: DeprecationWarning: datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.now(datetime.UTC).
    today = datetime.utcnow()

test/test_metar.py: 209 warnings
  /builddir/build/BUILDROOT/python-metar-1.10.0-1.fc39.x86_64/usr/lib/python3.12/site-packages/metar/Metar.py:420: DeprecationWarning: datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.now(datetime.UTC).
    self._now = datetime.datetime.utcnow()

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================ short test summary info =================================
FAILED test/test_metar.py::test_issue51_strict - assert 2 == 1
FAILED test/test_metar.py::test_not_strict_mode - assert 2 == 1
======================= 2 failed, 68 passed, 212 warnings in 0.41s =======================

After inspecting the failing tests and adding some test prints in them it became clear that a new DeprecationWarning is issued which confuses the warning count tested by these 2 tests.
For example the call to Metar.Metar(code, strict=False) in test test_not_strict_mode() now triggers 2 warnings in stead of just one. After inserting the lines

for warn in w:
        print('DEBUG: warn=', warn)

just after the with block I see this output:

'''
DEBUG: warn= {message : DeprecationWarning('datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.now(datetime.UTC).'), category : 'DeprecationWarning', filename : '/builddir/build/BUILDROOT/python-metar-1.10.0-1.fc39.x86_64/usr/lib/python3.12/site-packages/metar/Metar.py', lineno : 420, line : None}
DEBUG: warn= {message : RuntimeWarning("Unparsed groups in body 'M' while processing 'K9L2 100958Z AUTO 33006KT 10SM CLR M A3007 RMK AO2 SLPNO FZRANO $'"), category : 'RuntimeWarning', filename : '/builddir/build/BUILDROOT/python-metar-1.10.0-1.fc39.x86_64/usr/lib/python3.12/site-packages/metar/Metar.py', lineno : 502, line : None}
'''

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions