打字是否在文件中起作用?

2024-09-28 19:05:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我声明并键入一个带有可选参数的函数,该参数应该是数据帧。但是,我无法让python类型暗示提供任何警告/类型检查支持

这是在进入列类型之前,只是数据帧不是Dict级别的事实

# file 1

    def write_bq(self, df: pd.DataFrame = None, delete_first=True):

在另一个文件中,我用错误的类型来调用它。 但似乎没有任何支持

# file2 (actually a subclass) 

        updated: List[Dict] = []
        self.write_bq(updated)

除了在运行时,没有任何警告

pylint cxutils/digger/chat_stat.py 
--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

而且mypy只是抱怨标准导入库,但高兴地跳过了我的坏代码

mypy cxutils/digger/chat_stat.py 
cxutils/gbot.py:11: error: Skipping analyzing 'httplib2': found module but no type hints or library stubs
cxutils/logit.py:10: error: Skipping analyzing 'google.cloud': found module but no type hints or library stubs
# ... etc.

我是一个繁重的typescript用户,但在使用python类型提示时,除了基本类型之外,我总是遇到麻烦

对上述问题有何建议? 我想我在配置中遗漏了一些东西。我开始添加一个mypy.ini来尝试跳过缺少的配置,但这似乎没有得到应用。 在虚拟环境中运行,因此假设活动的venv mypy将先于任何全局which mypy证实了这一点

更新:我可以使用cmd行标志跳过导入,但仍然找不到错误

mypy cxutils/digger/chat_stat.py --ignore-missing-imports
Success: no issues found in 1 source file

通过--verbose我可以看到它确实在查看我的文件

(venv) dcollier@dcsan:~/dev/kzen$ mypy -v cxutils/digger/chat_stat.py --ignore-missing-imports

LOG:  Mypy Version:           0.812
LOG:  Config File:            mypy.ini
LOG:  Configured Executable:  /home/dcollier/dev/kzen/venv/bin/python3
LOG:  Current Executable:     /home/dcollier/dev/kzen/venv/bin/python3
LOG:  Cache Dir:              .mypy_cache
LOG:  Compiled:               True
LOG:  Exclude:                
LOG:  Found source:           BuildSource(path='cxutils/digger/chat_stat.py', module='cxutils.digger.chat_stat', has_text=False, base_dir='/home/dcollier/dev/kzen')
LOG:  Metadata fresh for cxutils.digger.chat_stat: file cxutils/digger/chat_stat.py
LOG:  Metadata fresh for cxutils.digger: file /home/dcollier/dev/kzen/cxutils/digger/__init__.py
LOG:  Metadata fresh for cxutils: file /home/dcollier/dev/kzen/cxutils/__init__.py

[更新2]

我尝试了一个非常简单的测试,将一个int传递给一个字符串,所以实际上,类型检查通常是完全错误的

(venv) dcollier@dcsan:~/dev/kzen$ which mypy
/home/dcollier/dev/kzen/venv/bin/mypy

(venv) dcollier@dcsan:~/dev/kzen$ mypy tests/type_hints_test.py 
Success: no issues found in 1 source file
(venv) dcollier@dcsan:~/dev/kzen$ cat tests/type_hints_test.py 

"""
testing type hinting
"""


def echo(msg: str):
    """simple echo"""
    assert isinstance(msg, str), 'Argument of wrong type!'
    print(msg)


def test_hints():
    """hints"""
    value: int = 10
    echo(value)



(venv) dcollier@dcsan:~/dev/kzen$ python -V
Python 3.9.2
(venv) dcollier@dcsan:~/dev/kzen$ mypy -V
mypy 0.812

注意,我在本地运行3.9,但计划在3.8上部署,因此尝试保持旧式List[x],而不是list(x)。并不是说我对类型提示的细节很熟悉

FWIW单元测试将使断言失败,但是类型提示没有发现任何错误


    def echo(msg: str):
        """simple echo"""
>       assert isinstance(msg, str), 'Argument of wrong type!'
E       AssertionError: Argument of wrong type!
E       assert False
E        +  where False = isinstance(10, str)

tests/type_hints_test.py:8: AssertionError
========================= short test summary info =========================
FAILED tests/type_hints_test.py::test_hints - AssertionError: Argument o...
============================ 1 failed in 0.08s ============================
(venv) dcollier@dcsan:~/dev/kzen$ mypy tests/type_hints_test.py 
Success: no issues found in 1 source file
(venv) dcollier@dcsan:~/dev/kzen$ 

Tags: pydevlog类型venvtypechatstat