英文:
TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'type'
问题
以下是翻译好的部分:
我正在运行我的Python项目的测试,除了一个之外,所有测试都通过了。
以下是函数的代码:
@botcmd
def reminder_next():
today = datetime.now()
return "\n".join(
f"Next planning: {Reminder.next_occurance('Sprint planning', today)}",
f"Next daily: {Reminder.next_daily(datetime.now(timezone), today)}",
f"Next review: {Reminder.next_occurance('Sprint review', today)}",
f"Next retrospective: {Reminder.next_occurance('Sprint restrospective', today)}"
)
以及测试的代码:
@freeze_time("2023-06-12", tz_offset=2)
def test_reminder_next():
expected_response = "\n".join([
f"Next planning: {datetime(2023, 6, 19, 15, 30, tzinfo=timezone_utc2)}",
f"Next daily: {datetime(2023, 6, 13, 9, 30, tzinfo=timezone_utc2)}",
f"Next review: {datetime(2023, 6, 15, 14, 45, tzinfo=timezone_utc2)}",
f"Next retrospective: {datetime(2023, 6, 16, 9, 30, tzinfo=timezone_utc2)}"
])
assert Reminder.reminder_next() == expected_response
以及错误信息:
TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'type'
顺便说一下,timezone_utc2
是由 timezone_utc2 = pytz.timezone('Etc/GMT+2')
定义的(已导入pytz)。
我尝试将 timezone_utc2
替换为 Etc/GMT+2
:错误末尾的类型从 'type' 变成了 'str'。
我尝试不添加任何东西,但也不起作用。
如果测试正确,应该会看到绿色的条形图,显示“7个测试通过,用时x秒”(我有其他6个测试也是正确的)。
英文:
I am running test for my project in Python and all of them passed, excepted one.
Here is the code of the function:
@botcmd
def reminder_next():
today = datetime.now()
return "\n".join(
f"Next planning: {Reminder.next_occurance('Sprint planning', today)}",
f"Next daily: {Reminder.next_daily(datetime.now(timezone), today)}",
f"Next review: {Reminder.next_occurance('Sprint review', today)}",
f"Next retrospective: {Reminder.next_occurance('Sprint restrospective', today)}"
)
And the code of the test:
@freeze_time("2023-06-12", tz_offset=2)
def test_reminder_next():
expected_response = "\n".join([
f"Next planning: {datetime(2023, 6, 19, 15, 30, tzinfo=timezone_utc2)}",
f"Next daily: {datetime(2023, 6, 13, 9, 30, tzinfo=timezone_utc2)}",
f"Next review: {datetime(2023, 6, 15, 14, 45, tzinfo=timezone_utc2)}",
f"Next retrospective: {datetime(2023, 6, 16, 9, 30, tzinfo=timezone_utc2)}"
])
assert Reminder.reminder_next() == expected_response
And the error:
>TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'type'
By the way timezone_utc2
is defined by timezone_utc2 = pytz.timezone('Etc/GMT+2')
(pytz is imported)
I tried replacing timezone_utc2
by Etc/GMT+2
: instead of 'type' at the end of the error, I got 'str'.
I tried without anything and it doesn't work either.
If the test is correct, I should have a green bar with '7 passed in x seconds' (I have 6 other tests which are correct)
答案1
得分: 0
try to use timezone_utc2.localize https://pypi.org/project/pytz/
@freeze_time("2023-06-12", tz_offset=2)
def test_reminder_next():
expected_response = "\n".join([
f"Next planning: {timezone_utc2.localize(datetime(2023, 6, 19, 15, 30))}",
f"Next daily: {timezone_utc2.localize(datetime(2023, 6, 13, 9, 30))}",
f"Next review: {timezone_utc2.localize(datetime(2023, 6, 15, 14, 45))}",
f"Next retrospective: {timezone_utc2.localize(datetime(2023, 6, 16, 9, 30))}"
])
assert Reminder.reminder_next() == expected_response
英文:
try to use timezone_utc2.localize https://pypi.org/project/pytz/
@freeze_time("2023-06-12", tz_offset=2)
def test_reminder_next():
expected_response = "\n".join([
f"Next planning: {timezone_utc2.localize(datetime(2023, 6, 19, 15, 30))}",
f"Next daily: {timezone_utc2.localize(datetime(2023, 6, 13, 9, 30))}",
f"Next review: {timezone_utc2.localize(datetime(2023, 6, 15, 14, 45))}",
f"Next retrospective: {timezone_utc2.localize(datetime(2023, 6, 16, 9, 30))}"
])
assert Reminder.reminder_next() == expected_response
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论