Skip to content

[attribute-error] when usage is protected by an if statement #1867

@cjw296

Description

@cjw296

I have this class method:

    @classmethod
    def _adjust_instance_using_tzinfo(cls, instance: datetime) -> datetime:
        if cls._mock_tzinfo:
            offset = cls._mock_tzinfo.utcoffset(instance)
            if offset is None:
                raise TypeError('tzinfo with .utcoffset() returning None is not supported')
            instance = instance - offset
        return instance

...where pytype complains but mypy does not, and it feels like the pytype complaint is erroneous:

[10/35] check testfixtures.datetime
FAILED: /Users/chris/vcs/git/testfixtures/.pytype/pyi/testfixtures/datetime.pyi 
/Users/chris/virtualenvs/testfixtures_312/bin/python -m pytype.main --disable import-error --imports_info /Users/chris/vcs/git/testfixtures/.pytype/imports/testfixtures.datetime.imports --module-name testfixtures.datetime --platform darwin -V 3.12 -o /Users/chris/vcs/git/testfixtures/.pytype/pyi/testfixtures/datetime.pyi --analyze-annotated --nofail --quick /Users/chris/vcs/git/testfixtures/testfixtures/datetime.py
/Users/chris/vcs/git/testfixtures/testfixtures/datetime.py:264:22: error: in _adjust_instance_using_tzinfo: No attribute 'utcoffset' on None [attribute-error]
  In Optional[datetime.tzinfo]

            offset = cls._mock_tzinfo.utcoffset(instance)
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~

Even if I change the guarding if block to be if cls._mock_tzinfo is not None, the complaint remains.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions