Skip to content

Commit

Permalink
Fix mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sffjunkie committed Nov 9, 2022
1 parent e46e51a commit bc6885b
Show file tree
Hide file tree
Showing 27 changed files with 98 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/astral/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
try:
import zoneinfo
except ImportError:
from backports import zoneinfo
from backports import zoneinfo # type: ignore


__all__ = [
Expand Down
9 changes: 4 additions & 5 deletions src/astral/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
try:
import zoneinfo
except ImportError:
from backports import zoneinfo
from backports import zoneinfo # type: ignore

options = argparse.ArgumentParser()
options.add_argument(
Expand Down Expand Up @@ -54,20 +54,19 @@
sun_as_str = {}
format_str = "%Y-%m-%dT%H:%M:%S"
if args.tzname is None:
tzinfo = datetime.timezone.utc
kwargs["tzinfo"] = datetime.timezone.utc
format_str += "Z"
else:
tzinfo = zoneinfo.ZoneInfo(loc.timezone)
kwargs["tzinfo"] = zoneinfo.ZoneInfo(loc.timezone)
format_str += "%z"

kwargs["tzinfo"] = tzinfo

s = sun.sun(**kwargs)

for key, value in s.items():
sun_as_str[key] = s[key].strftime(format_str)

sun_as_str["timezone"] = tzinfo.tzname
sun_as_str["timezone"] = kwargs["tzinfo"].tzname
sun_as_str["location"] = f"{loc.name}, {loc.region}"

print(json.dumps(sun_as_str))
6 changes: 3 additions & 3 deletions src/astral/julian.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ def _time_to_seconds(t: datetime.time) -> int:
year = at.year
month = at.month
day = at.day
day_fraction = 0
day_fraction = 0.0
if isinstance(at, datetime.datetime):
t = _time_to_seconds(at.time())
day_fraction = t / (24 * 60 * 60)
else:
day_fraction = 0
day_fraction = 0.0

if month <= 2:
year -= 1
Expand Down Expand Up @@ -102,7 +102,7 @@ def julianday_to_datetime(jd: float) -> datetime.datetime:
d = int(365.25 * c)
e = int((b - d) / 30.6001)

d = b - d - int(30.6001 * e) + f
d = b - d - int(30.6001 * e) + f # type: ignore
day = int(d)
t = d - day
total_seconds = t * (24 * 60 * 60)
Expand Down
2 changes: 1 addition & 1 deletion src/astral/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
try:
import zoneinfo
except ImportError:
from backports import zoneinfo
from backports import zoneinfo # type: ignore

from typing import Any, Dict, Optional, Tuple, Union

Expand Down
2 changes: 1 addition & 1 deletion src/astral/moon.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
try:
import zoneinfo
except ImportError:
from backports import zoneinfo
from backports import zoneinfo # type: ignore

from astral import AstralBodyPosition, Observer, now, today
from astral.julian import julianday, julianday_2000
Expand Down
4 changes: 2 additions & 2 deletions src/astral/sun.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
try:
import zoneinfo
except ImportError:
from backports import zoneinfo
from backports import zoneinfo # type: ignore

from astral import (
Depression,
Expand Down Expand Up @@ -444,7 +444,7 @@ def noon(
second,
tzinfo=datetime.timezone.utc,
)
return noon.astimezone(tzinfo) # type: ignore pylint: disable=E1120
return noon.astimezone(tzinfo) # type: ignore # pylint: disable=E1120


def midnight(
Expand Down
1 change: 0 additions & 1 deletion src/docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

source_suffix = ".rst"
master_doc = "index"
exclude_trees = []

pygments_style = "sphinx"

Expand Down
2 changes: 1 addition & 1 deletion src/test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pytest
import pytest # type: ignore

from astral import LocationInfo
from astral.geocoder import LocationDatabase, database
Expand Down
12 changes: 3 additions & 9 deletions src/test/moon/test_moon.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
# -*- coding: utf-8 -*-
import datetime

import pytest

try:
import zoneinfo
except ImportError:
from backports import zoneinfo

import pytest # type: ignore
from almost_equal import datetime_almost_equal

from astral import moon
Expand Down Expand Up @@ -107,8 +101,8 @@ def test_moonset_riyadh_utc(
def test_moonrise_wellington(
date_: datetime.date, risetime: datetime.datetime, wellington: Location
):
risetime = risetime.replace(tzinfo=wellington.tz)
calc_time = moon.moonrise(wellington.observer, date_, tzinfo=wellington.tz)
risetime = risetime.replace(tzinfo=wellington.tzinfo)
calc_time = moon.moonrise(wellington.observer, date_, tzinfo=wellington.tzinfo)
assert calc_time is not None
calc_time = calc_time.astimezone(wellington.tzinfo)
assert datetime_almost_equal(calc_time, risetime, seconds=120)
Expand Down
2 changes: 1 addition & 1 deletion src/test/moon/test_moon_azimuth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime

import pytest
import pytest # type: ignore

from astral import Observer
from astral.location import Location
Expand Down
4 changes: 2 additions & 2 deletions src/test/moon/test_moon_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ def test_moon_position():
d = date(1969, 6, 28)
jd = julianday(d)
jd2000 = jd - 2451545 # Julian day relative to Jan 1.5, 2000
_pos1 = moon_position(jd2000)
moon_position(jd2000)

d = date(1992, 4, 12)
jd = julianday(d)
jd2000 = jd - 2451545 # Julian day relative to Jan 1.5, 2000
_pos2 = moon_position(jd2000)
moon_position(jd2000)
pass


Expand Down
22 changes: 11 additions & 11 deletions src/test/test_Location.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
try:
import zoneinfo
except ImportError:
from backports import zoneinfo
from backports import zoneinfo # type: ignore

import freezegun
import pytest
import pytest # type: ignore
from almost_equal import datetime_almost_equal

from astral import LocationInfo
Expand Down Expand Up @@ -57,13 +57,13 @@ def test_Info(self, london: Location, london_info: LocationInfo):

def test_Sun(self, london: Location):
"""Test Location's version of the sun calculation"""
ldt = datetime.datetime(2015, 8, 1, 5, 23, 20, tzinfo=london.tz)
ldt = datetime.datetime(2015, 8, 1, 5, 23, 20, tzinfo=london.tzinfo)
sunrise = london.sun(datetime.date(2015, 8, 1))["sunrise"]
assert datetime_almost_equal(sunrise, ldt)

def test_Dawn(self, london: Location):
"""Test Location returns dawn times in the local timezone"""
ldt = datetime.datetime(2015, 8, 1, 4, 41, 44, tzinfo=london.tz)
ldt = datetime.datetime(2015, 8, 1, 4, 41, 44, tzinfo=london.tzinfo)
dawn = london.dawn(datetime.date(2015, 8, 1))
assert datetime_almost_equal(dawn, ldt)
# assert dawn.tzinfo.zone == london.tzinfo.zone
Expand All @@ -76,7 +76,7 @@ def test_DawnUTC(self, london: Location):
# assert dawn.tzinfo.zone == datetime.timezone.utc.zone

def test_Sunrise(self, london: Location):
ldt = datetime.datetime(2015, 8, 1, 5, 23, 20, tzinfo=london.tz)
ldt = datetime.datetime(2015, 8, 1, 5, 23, 20, tzinfo=london.tzinfo)
sunrise = london.sunrise(datetime.date(2015, 8, 1))
assert datetime_almost_equal(sunrise, ldt)
# assert sunrise.tzinfo.zone == london.tzinfo.zone
Expand All @@ -88,7 +88,7 @@ def test_SunriseUTC(self, london: Location):
# assert sunrise.tzinfo.zone == datetime.timezone.utc.zone

def test_SolarNoon(self, london: Location):
ldt = datetime.datetime(2015, 8, 1, 13, 6, 53, tzinfo=london.tz)
ldt = datetime.datetime(2015, 8, 1, 13, 6, 53, tzinfo=london.tzinfo)
noon = london.noon(datetime.date(2015, 8, 1))
assert datetime_almost_equal(noon, ldt)
# assert noon.tzinfo.zone == london.tzinfo.zone
Expand All @@ -100,7 +100,7 @@ def test_SolarNoonUTC(self, london: Location):
# assert noon.tzinfo.zone == datetime.timezone.utc.zone

def test_Dusk(self, london: Location):
ldt = datetime.datetime(2015, 12, 1, 16, 35, 11, tzinfo=london.tz)
ldt = datetime.datetime(2015, 12, 1, 16, 35, 11, tzinfo=london.tzinfo)
dusk = london.dusk(datetime.date(2015, 12, 1))
assert datetime_almost_equal(dusk, ldt)
# assert dusk.tzinfo.zone == london.tzinfo.zone
Expand All @@ -112,7 +112,7 @@ def test_DuskUTC(self, london: Location):
# assert dusk.tzinfo.zone == datetime.timezone.utc.zone

def test_Sunset(self, london: Location):
ldt = datetime.datetime(2015, 12, 1, 15, 55, 29, tzinfo=london.tz)
ldt = datetime.datetime(2015, 12, 1, 15, 55, 29, tzinfo=london.tzinfo)
sunset = london.sunset(datetime.date(2015, 12, 1))
assert datetime_almost_equal(sunset, ldt)
# assert sunset.tzinfo.zone == london.tzinfo.zone
Expand All @@ -124,20 +124,20 @@ def test_SunsetUTC(self, london: Location):
# assert sunset.tzinfo.zone == datetime.timezone.utc.zone

def test_SolarElevation(self, riyadh: Location):
dt = datetime.datetime(2015, 12, 14, 8, 0, 0, tzinfo=riyadh.tz)
dt = datetime.datetime(2015, 12, 14, 8, 0, 0, tzinfo=riyadh.tzinfo)
elevation = riyadh.solar_elevation(dt)
assert abs(elevation - 17) < 0.5

def test_SolarAzimuth(self, riyadh: Location):
dt = datetime.datetime(2015, 12, 14, 8, 0, 0, tzinfo=riyadh.tz)
dt = datetime.datetime(2015, 12, 14, 8, 0, 0, tzinfo=riyadh.tzinfo)
azimuth = riyadh.solar_azimuth(dt)
assert abs(azimuth - 126) < 0.5

def test_TimeAtAltitude(self, new_delhi: Location):
test_data = {datetime.date(2016, 1, 5): datetime.datetime(2016, 1, 5, 10, 0)}

for day, cdt in test_data.items():
cdt = cdt.replace(tzinfo=new_delhi.tz)
cdt = cdt.replace(tzinfo=new_delhi.tzinfo)
dt = new_delhi.time_at_elevation(28, day)
assert datetime_almost_equal(dt, cdt, seconds=600)

Expand Down
26 changes: 20 additions & 6 deletions src/test/test_Repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,29 @@

class TestLocationRepr:
def test_default(self):
l = Location()
location = Location()
assert (
l.__repr__() == "Greenwich/England, tz=Europe/London, lat=51.47, lon=-0.00"
location.__repr__()
== "Greenwich/England, tz=Europe/London, lat=51.47, lon=-0.00"
)

def test_full(self):
l = Location(LocationInfo("London", "England", "Europe/London", 51.68, -0.05))
assert l.__repr__() == "London/England, tz=Europe/London, lat=51.68, lon=-0.05"
location = Location(
LocationInfo("London", "England", "Europe/London", 51.68, -0.05)
)
assert (
location.__repr__()
== "London/England, tz=Europe/London, lat=51.68, lon=-0.05"
)

def test_no_region(self):
l = Location(LocationInfo("London", None, "Europe/London", 51.68, -0.05))
assert l.__repr__() == "London, tz=Europe/London, lat=51.68, lon=-0.05"
location = Location(
LocationInfo(
"London",
None,
"Europe/London",
51.68,
-0.05,
)
)
assert location.__repr__() == ("London, tz=Europe/London, lat=51.68, lon=-0.05")
2 changes: 1 addition & 1 deletion src/test/test_depression_not_reached.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import datetime

import pytest
import pytest # type: ignore

from astral import LocationInfo
from astral.location import Location
Expand Down
2 changes: 1 addition & 1 deletion src/test/test_geocoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
try:
import zoneinfo
except ImportError:
from backports import zoneinfo
from backports import zoneinfo # type: ignore

from pytest import approx, raises # type: ignore

Expand Down
1 change: 1 addition & 0 deletions src/test/test_julian.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# type: ignore
import datetime
from typing import Union

Expand Down
3 changes: 2 additions & 1 deletion src/test/test_location_info.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# type: ignore
import pytest

from astral import LocationInfo

try:
import zoneinfo
except ImportError:
from backports import zoneinfo
from backports import zoneinfo # type: ignore


class TestLocationInfo:
Expand Down
3 changes: 2 additions & 1 deletion src/test/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# type: ignore
from datetime import timedelta

try:
import zoneinfo
except ImportError:
from backports import zoneinfo
from backports import zoneinfo # type: ignore

import freezegun
from pytest import approx, raises
Expand Down
2 changes: 1 addition & 1 deletion src/test/test_norway.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime, timedelta, timezone

import pytest
import pytest # type: ignore

import astral
from astral import sun
Expand Down
1 change: 1 addition & 0 deletions src/test/test_observer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# type: ignore
import pytest

from astral import Observer
Expand Down
Loading

0 comments on commit bc6885b

Please sign in to comment.