- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么 pylint 和 IDE 的智能感知功能无法识别 time.struct_time
的实例?以下代码包含对类的存在/不存在属性、命名元组和类似命名元组的 time.struct_time
的一些简单测试。在 pylint、IntelliJ 和 VSCode 中,一切都按预期工作 - 除了 time.struct_time
之外,在每种情况下都会报告对缺失属性的访问 - 它不会在任何这些工具中生成警告或错误。为什么他们不能说出它是什么以及它的属性是什么?
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
问题的上下文是 pipenv
中的以下最新错误 -
# Halloween easter-egg.
if ((now.tm_mon == 10) and (now.tm_day == 30))
显然,传递路径从未经过测试,但典型的静态分析工具似乎也无济于事。这对于标准库中的类型来说很奇怪。
(可以在 https://github.com/kennethreitz/pipenv/commit/033b969d094ba2d80f8ae217c8c604bc40160b03 看到完整的修复)
最佳答案
time.struct_time
是用 C 语言定义的对象,这意味着它不能被静态地自省(introspection)。自动完成软件可以解析 Python 代码并合理猜测支持哪些类和命名元组,但它们不能对 C 定义的对象执行此操作。
大多数系统使用的解决方法是生成 stub 文件;通常通过在运行时内省(introspection)对象(导入模块并记录找到的属性)。例如,CodeIntel(Komodo IDE 的一部分)使用 XML file format called CIX .但是,这更容易出错,因此这样的系统宁可谨慎行事,也不会明确地将未知属性标记为错误。
如果您使用 Python 3 编写代码,您可以考虑使用 type hinting .对于 C 扩展,您仍然需要 stub 文件,但社区现在非常擅长维护这些文件。标准库 stub 文件保存在 project called 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
然后是 flake8 tool结合 flake8-mypy plugin将检测不良属性:
$ 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 也建立在这项工作的基础上,也许可以检测到同样的无效使用。肯定是directly supports pyi files .
关于python-3.x - time.struct_time 难以忍受的不透明性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46594779/
您可以在 Python 中使用 strptime 方法解析表示时间的字符串。 stackoverflow 上有许多工作示例: Converting string into datetime 但是,如果
我的格式是这样的: (Pdb) aa = time.strptime("2015-02-28 14:19:05.512", "%Y-%m-%d %H:%M:%S.%f") (Pdb) aa time.
一个比另一个更受欢迎吗?如果是,是在所有情况下还是在少数情况下? 我打算使用某种形式的日期类来保存日期和时间数据的长列表,例如'2009-01-01 10:12:00'. 最佳答案 struct_ti
在 python 中处理时间和日期时,您会偶然发现 time.struct_time 对象: st = time.strptime("23.10.2012", "%d.%m.%Y") print st
为什么 pylint 和 IDE 的智能感知功能无法识别 time.struct_time 的实例?以下代码包含对类的存在/不存在属性、命名元组和类似命名元组的 time.struct_time 的一
我正在编写一个获取 2 个变量、showdate 和 viewtype 的代码。变量数据通过 JavaScript 通过 POST 方法发送。 viewtype = send an srt showd
这是给出错误的python脚本: >>> import time >>> t=[ ] >>> t.append(time.struct_time(tm_year=2000, tm_mon
在 Python 中解析日期后,我需要对其进行修补。但结构体 time.struct_time 仅具有只读属性: parsed = time.strptime("23:59", "%H:%M") pa
当我转换这个时 time.struct_time(tm_year=2014, tm_mon=11, tm_mday=30, tm_hour=4, tm_min=50, tm_sec=8, tm_wda
我是 python 的新手,我不明白如何正确格式化日期。 我的数据是这样的 Fri, 09 Dec 2011 06:50:37 UTC 我是这样准备的: dates.append(time.strpt
如何将 Python time.struct_time 对象转换为 datetime.datetime 对象? 我有一个提供第一个的库和一个需要第二个的库。 最佳答案 使用 time.mktime()
我正在使用 python 的 feedparser 模块来解析 RSS 提要。解析后,feedparser 以 python 9 元组时间格式 (time.struct_time) 返回日期。 我想将
我有一个 python 脚本,我需要比较两个日期。我有一个日期列表作为 time.struct_time 对象,我需要将其与几个 datetime.date 对象进行比较。 如何将 datetime.
我有一个 Python 对象: time.struct_time(tm_year=2013, tm_mon=10, tm_mday=11, tm_hour=11, tm_min=57, tm_sec=
我在传递 datetime 时遇到问题进入Pandas.Series.asof : def valueAsOf(self, date): if type(date) is str:
我有一个最奇怪的问题。此 Python 代码适用于运行股票 Debian 的 Raspberry Pi: from datetime import datetime, date, time now =
我使用带有 Flask 的 Postgresql 使用 SQLAlchemy 有以下数据库结构: class Attendance(db.Model): id = db.Column(db.Int
我是一名优秀的程序员,十分优秀!