TypeError: tzinfo参数必须为None或tzinfo子类,而不是’type’类型。

huangapple go评论186阅读模式
英文:

TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'type'

问题

以下是翻译好的部分:

  1. 我正在运行我的Python项目的测试除了一个之外所有测试都通过了
  2. 以下是函数的代码
  3. @botcmd
  4. def reminder_next():
  5. today = datetime.now()
  6. return "\n".join(
  7. f"Next planning: {Reminder.next_occurance('Sprint planning', today)}",
  8. f"Next daily: {Reminder.next_daily(datetime.now(timezone), today)}",
  9. f"Next review: {Reminder.next_occurance('Sprint review', today)}",
  10. f"Next retrospective: {Reminder.next_occurance('Sprint restrospective', today)}"
  11. )

以及测试的代码:

  1. @freeze_time("2023-06-12", tz_offset=2)
  2. def test_reminder_next():
  3. expected_response = "\n".join([
  4. f"Next planning: {datetime(2023, 6, 19, 15, 30, tzinfo=timezone_utc2)}",
  5. f"Next daily: {datetime(2023, 6, 13, 9, 30, tzinfo=timezone_utc2)}",
  6. f"Next review: {datetime(2023, 6, 15, 14, 45, tzinfo=timezone_utc2)}",
  7. f"Next retrospective: {datetime(2023, 6, 16, 9, 30, tzinfo=timezone_utc2)}"
  8. ])
  9. 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:

  1. @botcmd
  2. def reminder_next():
  3. today = datetime.now()
  4. return "\n".join(
  5. f"Next planning: {Reminder.next_occurance('Sprint planning', today)}",
  6. f"Next daily: {Reminder.next_daily(datetime.now(timezone), today)}",
  7. f"Next review: {Reminder.next_occurance('Sprint review', today)}",
  8. f"Next retrospective: {Reminder.next_occurance('Sprint restrospective', today)}"
  9. )

And the code of the test:

  1. @freeze_time("2023-06-12", tz_offset=2)
  2. def test_reminder_next():
  3. expected_response = "\n".join([
  4. f"Next planning: {datetime(2023, 6, 19, 15, 30, tzinfo=timezone_utc2)}",
  5. f"Next daily: {datetime(2023, 6, 13, 9, 30, tzinfo=timezone_utc2)}",
  6. f"Next review: {datetime(2023, 6, 15, 14, 45, tzinfo=timezone_utc2)}",
  7. f"Next retrospective: {datetime(2023, 6, 16, 9, 30, tzinfo=timezone_utc2)}"
  8. ])
  9. 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

  1. try to use timezone_utc2.localize https://pypi.org/project/pytz/
  2. @freeze_time("2023-06-12", tz_offset=2)
  3. def test_reminder_next():
  4. expected_response = "\n".join([
  5. f"Next planning: {timezone_utc2.localize(datetime(2023, 6, 19, 15, 30))}",
  6. f"Next daily: {timezone_utc2.localize(datetime(2023, 6, 13, 9, 30))}",
  7. f"Next review: {timezone_utc2.localize(datetime(2023, 6, 15, 14, 45))}",
  8. f"Next retrospective: {timezone_utc2.localize(datetime(2023, 6, 16, 9, 30))}"
  9. ])
  10. assert Reminder.reminder_next() == expected_response
英文:

try to use timezone_utc2.localize https://pypi.org/project/pytz/

  1. @freeze_time("2023-06-12", tz_offset=2)
  2. def test_reminder_next():
  3. expected_response = "\n".join([
  4. f"Next planning: {timezone_utc2.localize(datetime(2023, 6, 19, 15, 30))}",
  5. f"Next daily: {timezone_utc2.localize(datetime(2023, 6, 13, 9, 30))}",
  6. f"Next review: {timezone_utc2.localize(datetime(2023, 6, 15, 14, 45))}",
  7. f"Next retrospective: {timezone_utc2.localize(datetime(2023, 6, 16, 9, 30))}"
  8. ])
  9. assert Reminder.reminder_next() == expected_response

huangapple
  • 本文由 发表于 2023年6月15日 14:59:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定