From 01263c2155e48ca586d6430e4f9674e0755ca181 Mon Sep 17 00:00:00 2001 From: Mark Campanelli Date: Mon, 23 Dec 2024 14:40:25 -0700 Subject: [PATCH] Type Location's tz and pytz attributes as advertised --- pvlib/location.py | 10 ++++++---- pvlib/tools.py | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pvlib/location.py b/pvlib/location.py index 9259f410fa..e3ad7e6db1 100644 --- a/pvlib/location.py +++ b/pvlib/location.py @@ -73,10 +73,10 @@ def __init__(self, latitude, longitude, tz='UTC', altitude=None, self.pytz = pytz.UTC elif isinstance(tz, datetime.tzinfo): self.tz = tz.zone - self.pytz = tz + self.pytz = pytz.timezone(tz.zone) elif isinstance(tz, (int, float)): - self.tz = tz - self.pytz = pytz.FixedOffset(tz*60) + self.tz = f"Etc/GMT{int(-tz):+d}" + self.pytz = pytz.timezone(self.tz) else: raise TypeError('Invalid tz specification') @@ -89,8 +89,10 @@ def __init__(self, latitude, longitude, tz='UTC', altitude=None, def __repr__(self): attrs = ['name', 'latitude', 'longitude', 'altitude', 'tz'] + # Use None as getattr default in case __repr__ is called during + # initialization before all attributes have been assigned. return ('Location: \n ' + '\n '.join( - f'{attr}: {getattr(self, attr)}' for attr in attrs)) + f'{attr}: {getattr(self, attr, None)}' for attr in attrs)) @classmethod def from_tmy(cls, tmy_metadata, tmy_data=None, **kwargs): diff --git a/pvlib/tools.py b/pvlib/tools.py index 86c2df937d..535b197a25 100644 --- a/pvlib/tools.py +++ b/pvlib/tools.py @@ -133,7 +133,7 @@ def localize_to_utc(time, location): """ if isinstance(time, dt.datetime): if time.tzinfo is None: - time = pytz.timezone(location.tz).localize(time) + time = location.pytz.localize(time) time_utc = time.astimezone(pytz.utc) else: try: