ValueError when importing unicode_literals from __future__

Bug #1588198 reported by Hugo
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
astral
Fix Released
High
Simon Kennedy

Bug Description

Windows 7, Python 2.7.11, Astral 1.1

This code works (from the example code at https://pythonhosted.org/astral/#example ):

```
#!/usr/bin/env python
# encoding: utf-8
from __future__ import print_function
# from __future__ import unicode_literals
import datetime
from astral import Astral # pip install astral

city_name = 'London'
a = Astral()
a.solar_depression = 'civil'
city = a[city_name]

print('Information for %s/%s\n' % (city_name, city.region))

timezone = city.timezone
print('Timezone: %s' % timezone)

print(' Latitude: %.02f; Longitude: %.02f\n' % \
    (city.latitude, city.longitude))

sun = city.sun(date=datetime.date(2009, 4, 22), local=True)
print('Dawn: %s' % str(sun['dawn']))
print('Sunrise: %s' % str(sun['sunrise']))
print('Noon: %s' % str(sun['noon']))
print('Sunset: %s' % str(sun['sunset']))
print('Dusk: %s' % str(sun['dusk']))
```

Outputs:
```
M:\bin>python test.py
Information for London/England

Timezone: Europe/London
 Latitude: 51.50; Longitude: -0.12

Dawn: 2009-04-22 05:12:06+01:00
Sunrise: 2009-04-22 05:48:46+01:00
Noon: 2009-04-22 12:58:56+01:00
Sunset: 2009-04-22 20:09:00+01:00
Dusk: 2009-04-22 20:45:39+01:00

```

But uncommenting `from __future__ import unicode_literals` it gives:
```
M:\bin>python test.py
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    a.solar_depression = 'civil'
  File "C:\Python27\lib\site-packages\astral.py", line 1493, in solar_depression
    self._depression = float(depression)
ValueError: could not convert string to float: civil
```

Revision history for this message
Hugo (hugovk) wrote :

One fix:

Change:
    def solar_depression(self, depression):
        if isinstance(depression, str):

to:
    def solar_depression(self, depression):
        if isinstance(depression, str) or isinstance(depression, unicode):

---

Another fix:

Add as first import:
from __future__ import unicode_literals

In latitude(self, latitude), longitude(self, longitude), solar_depression(self, depression):

Change:
        if isinstance(depression, str):

to:
        if isinstance(depression, unicode):

---

For both, there may be other similar changes to make elsewhere in the code; tests should find them.

Changed in astral:
status: New → Confirmed
importance: Undecided → High
assignee: nobody → Simon Kennedy (sffjunkie)
Changed in astral:
status: Confirmed → Triaged
Changed in astral:
status: Triaged → Fix Committed
Revision history for this message
Hugo (hugovk) wrote :

Please let me know when this has been released.

Changed in astral:
status: Fix Committed → Fix Released
Revision history for this message
Hugo (hugovk) wrote :

Thanks, confirmed fixed with Astral 1.2.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.