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

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

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

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:

确定