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

Exception 'NoneType' object has no attribute 'replace' when parsing specific dates #840

Open
2 tasks done
ssssam opened this issue Aug 29, 2024 · 4 comments
Open
2 tasks done

Comments

@ssssam
Copy link

ssssam commented Aug 29, 2024

  • I am on the latest Pendulum version.
  • I have searched the issues of this repo and believe that this is not a duplicate.
  • Fedora 40:
  • 3.0.0

Issue

An exception is raised when parsing specific date times.

Current date time on my machine, in case that is relevant.

>>> pendulum.now()                                                                                                                                                                                                  
DateTime(2024, 8, 29, 16, 46, 14, 16970, tzinfo=Timezone('Europe/London')) 

Example of the bad case:

>>> pendulum.parse('Tue, 02 Mar 2024 06:00:00 GMT', tz="UTC", strict=False)                                                                                                                                         
Traceback (most recent call last):                                                                                                                                                                                  
  File "<stdin>", line 1, in <module>                                                                                                                                                                               
  File "/home/sam/flexget.venv/lib64/python3.12/site-packages/pendulum/parser.py", line 30, in parse                                                                                                                
    return _parse(text, **options)                                                                                                                                                                                  
           ^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                  
  File "/home/sam/flexget.venv/lib64/python3.12/site-packages/pendulum/parser.py", line 46, in _parse                                                                                                               
    return pendulum.datetime(                                                                                                                                                                                       
           ^^^^^^^^^^^^^^^^^^                                                                                                                                                                                       
  File "/home/sam/flexget.venv/lib64/python3.12/site-packages/pendulum/__init__.py", line 143, in datetime                                                                                                          
    return DateTime.create(                                                                                                                                                                                         
           ^^^^^^^^^^^^^^^^                                                                                                                                                                                         
  File "/home/sam/flexget.venv/lib64/python3.12/site-packages/pendulum/datetime.py", line 103, in create                                                                                                            
    tz = pendulum._safe_timezone(tz)                                                                                                                                                                                
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                
  File "/home/sam/flexget.venv/lib64/python3.12/site-packages/pendulum/__init__.py", line 112, in _safe_timezone                                                                                                    
    elif obj.tzname(None) == "UTC":                                                                                                                                                                                 
         ^^^^^^^^^^^^^^^^                                                                                                                                                                                           
  File "/home/sam/flexget.venv/lib64/python3.12/site-packages/dateutil/tz/tz.py", line 238, in tzname                                                                                                               
    return self._tznames[self._isdst(dt)]                                                                                                                                                                           
                         ^^^^^^^^^^^^^^^                                                                                                                                                                            
  File "/home/sam/flexget.venv/lib64/python3.12/site-packages/dateutil/tz/tz.py", line 291, in _isdst                                                                                                               
    dstval = self._naive_is_dst(dt)                                                                                                                                                                                 
             ^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                 
  File "/home/sam/flexget.venv/lib64/python3.12/site-packages/dateutil/tz/tz.py", line 259, in _naive_is_dst                                                                                                        
    timestamp = _datetime_to_timestamp(dt)                                                                                                                                                                          
                ^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                          
  File "/home/sam/flexget.venv/lib64/python3.12/site-packages/dateutil/tz/tz.py", line 1814, in _datetime_to_timestamp                                                                                              
    return (dt.replace(tzinfo=None) - EPOCH).total_seconds()                                                                                                                                                        
            ^^^^^^^^^^                                                                                                                                                                                              
AttributeError: 'NoneType' object has no attribute 'replace'                                                                                                                                                        

Example of the good case:

>>> pendulum.parse('Tue, 02 Apr 2024 06:00:00 GMT', tz="UTC", strict=False)                                                                                                                                         
DateTime(2024, 4, 2, 6, 0, 0, tzinfo=Timezone('UTC'))          

This is affecting Flexget rss plugin, see: Flexget/Flexget#4036

@ssssam ssssam changed the title Exception 'NoneType' object has no attribute 'replace'` when parsing specific dates Exception 'NoneType' object has no attribute 'replace' when parsing specific dates Aug 29, 2024
@M117n
Copy link

M117n commented Aug 31, 2024

Im gonna try to work on this

@M117n
Copy link

M117n commented Aug 31, 2024

I have tried to replicate the error using the same date and timezone configuration (Europe/London) in a Windows environment:

Operating System: Windows 11
Python: 3.9
Pendulum: 3.0.0
Date and time used: DateTime(2024, 8, 29, 16, 46, 14, 16970, tzinfo=Timezone('Europe/London'))
Parsing result: 2024-03-02 06:00:00+00:00 without any errors.

This suggests that the issue might be specific to the Fedora 40 environment or related to differences in dependency configurations on that operating system.

@ssssam
Copy link
Author

ssssam commented Aug 31, 2024

Thanks for confirming. Let me get a full ouptut of all package versions used in the venv and we can compare what is different. Here's output of pip3 freeze in the venv where I can reproduce this:

pendulum @ file:///home/sam/src/pendulum
python-dateutil==2.9.0.post0
six==1.16.0
time-machine==2.15.0
tzdata==2024.1

edit: I should also note the exact python version, which is this:

Name        : python3
Version     : 3.12.5
Release     : 1.fc40

It could also be a difference between Python 3.9 and Python 3.12 based on your results

@ashirley
Copy link

Note that the TZ environment variable has an impact here. On debian bookworm I see the following:

root@48ff42b03f9d:/# unset TZ
root@48ff42b03f9d:/# python3 -c 'import pendulum; print(pendulum.parse("Tue, 02 Mar 2024 06:00:00 GMT", tz="UTC", strict=False))'
2024-03-02 06:00:00+00:00
root@48ff42b03f9d:/# TZ="UTC" python3 -c 'import pendulum; print(pendulum.parse("Tue, 02 Mar 2024 06:00:00 GMT", tz="UTC", strict=False))'
2024-03-02 06:00:00+00:00
root@48ff42b03f9d:/# TZ="Europe/London" python3 -c 'import pendulum; print(pendulum.parse("Tue, 02 Mar 2024 06:00:00 GMT", tz="UTC", strict=False))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.11/dist-packages/pendulum/parser.py", line 30, in parse
    return _parse(text, **options)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pendulum/parser.py", line 46, in _parse
    return pendulum.datetime(
           ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pendulum/__init__.py", line 143, in datetime
    return DateTime.create(
           ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pendulum/datetime.py", line 103, in create
    tz = pendulum._safe_timezone(tz)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pendulum/__init__.py", line 112, in _safe_timezone
    elif obj.tzname(None) == "UTC":
         ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/dateutil/tz/tz.py", line 238, in tzname
    return self._tznames[self._isdst(dt)]
                         ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/dateutil/tz/tz.py", line 291, in _isdst
    dstval = self._naive_is_dst(dt)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/dateutil/tz/tz.py", line 259, in _naive_is_dst
    timestamp = _datetime_to_timestamp(dt)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/dateutil/tz/tz.py", line 1814, in _datetime_to_timestamp
    return (dt.replace(tzinfo=None) - EPOCH).total_seconds()
            ^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'replace'

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

No branches or pull requests

3 participants