The unbearable opacity of time. Struct_time

Why can't pylint and intellisense for IDE recognize instances time.struct_time? The following code contains some trivial tests of existing / nonexistent class attributes called tuples and named tuple-like time.struct_time. Everything works as expected in pylint, IntelliJ and VSCode - access to missing attributes is reported in each case, except time.struct_time- it does not generate any warnings or errors in any of these tools. Why can't they say what it is and what its attributes are?

import time
from collections import namedtuple

t = time.localtime()
e = t.tm_mday
e = t.bad # this is not reported by linters or IDEs. 

class Clz:
    cvar = 'whee'

    def __init__(self):
        self.ivar = 'whaa'

o = Clz()
e = Clz.cvar
e = o.ivar
e = Clz.bad
e = o.bad

Ntup = namedtuple('Ntup', 'thing')
n = Ntup(thing=3)
e = n.thing
e = n.bad

Question context - The following recent mistake is in pipenv-

# Halloween easter-egg.          
if ((now.tm_mon == 10) and (now.tm_day == 30)) 

, , , , . .

( https://github.com/kennethreitz/pipenv/commit/033b969d094ba2d80f8ae217c8c604bc40160b03)

+4
1

time.struct_time - , C, , . Python , namedtuples , C- .

- -; ( ). , CodeIntel ( IDE Komodo) XML CIX. , .

Python 3, hinting. C -, . - typeshed.

:

#!/usr/bin/env python3
import time
from collections import namedtuple


t: time.struct_time = time.localtime()
e: int = t.tm_mday
e = t.bad  # this is not reported by linters or IDEs.


class Clz:
    cvar: str = 'whee'
    ivar: str

    def __init__(self) -> None:
        self.ivar = 'whaa'


o = Clz()
s = Clz.cvar
s = o.ivar
s = Clz.bad
s = o.bad

Ntup = namedtuple('Ntup', 'thing')
n = Ntup(thing=3)
e = n.thing
e = n.bad

but then the flake8 tool combined with the flake8-mypy plugin will detect bad attributes:

$ flake8 test.py
test.py:8:5: T484 "struct_time" has no attribute "bad"
test.py:22:5: T484 "Clz" has no attribute "bad"
test.py:23:5: T484 "Clz" has no attribute "bad"
test.py:28:5: T484 "Ntup" has no attribute "bad"

PyCharm is also building this work and may possibly find the same invalid use. Of course, it directly supports pyi files .

+3
source

Source: https://habr.com/ru/post/1686985/


All Articles