问题标签 [type-annotation]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
121 浏览

function - F# 函数参数类型注释不起作用

我想为 F# 函数中的参数指定一个特定类型:

我希望函数的类型是:

但是函数类型是:

我应该怎么做才能强制参数 arg1 的类型为“NewType”?

0 投票
1 回答
2726 浏览

python - 如何向 asyncio.Task 添加类型注释

我有一个看起来像这样的代码:

我正在尝试向some_callback函数添加类型注释,但我无法完全理解对result变量进行注释的锄头。应该是Coroutine吗?或者也许Awaitable

当我使用reveal_typeof 时mypy,关于result变量的输出是Any.

这个程序的输出是:

我应该如何正确记录此功能?

0 投票
1 回答
226 浏览

xtext - 如何在 xtext 中使用类型注释?

如何使用 JvmModelInferrer 添加如下字段?

我已经拥有的是:

产生这个java代码:

但我不知道如何将IdInstance注释添加到类型中。

注意:将注释添加到该字段有效,但不是我想要的:

产生:

0 投票
1 回答
100 浏览

python-3.6 - 如何将函数参数键入为本机函数

我在 python repl 中使用了一个辅助函数来将变量移动到全局以便于调试。但是有一个mypy错误:

现在,线

引发一个 mypy 错误,说 func 没有 __defaults__ 现在我知道 BDFL 说他鄙视“hasattr”检查,所以它可能不会在 mypy 中解决;那么我的问题是,有没有办法更改 __init__ 键入签名以消除错误?

我尝试了什么:Callable 不起作用,可以理解:并非所有 Callables 都有 __defaults__。但是类型“函数”在哪里?如果我键入()一个函数,它会说“函数”,但“函数”不在序言或“打字”中。我看到有些人提到“FunctionType”,但也没有在“打字”中。

0 投票
2 回答
1183 浏览

java - 声明注解和类型注解的区别

Java 8 在 JSR308 中引入了类型注解。根据 Java 语言规范

类型注释可以在任何使用类型的地方使用,例如声明、泛型参数、强制转换等。

我对Java比较陌生,Java 8是我使用的第一个Java版本,所以我不熟悉非“类型注释”,即声明注释。

声明注解与类型注解有何不同?我想知道,因为我一直在手册中听到它们,看起来“类型注释”是“声明注释”的超集。

0 投票
2 回答
98 浏览

swift - 如何将字符类型注释转换为字符串类型注释?

将 Character 类型注解转换为 String 类型注解的目的是为了连接。

这里有一些答案,但是它没有帮助,因为它只显示了如何从字符串转换为字符。

我试过用String()构造函数来做,但没有用。

假设我有这段代码。

上面的代码会返回一个错误说Cannot convert value of type "Character" to expected argument type "String"

但是,如果我使用andappend()的串联工作就好了。namenameLastCha

结果是Tony完美的工作。

0 投票
1 回答
1829 浏览

python - How can I get mypy to either recognize or ignore dynamically imported types?

I have a Python project with the following structure

The contents of src/m/__init__.py are

The contents of src/m/a.py are

The contents of src/m/b.py are

When I run mypy against this code I get

error: Module 'src.m.a' has no attribute 'A'

I'm not sure if there's a way to get mypy to recognize this module. I understand that I'm dynamically creating and importing a type, so I don't necessarily expect mypy to work here, but I also don't know a way to get mypy to ignore it. I tried adding # type: ignore, but mypy says misplaced type annotation.

At this point, I can't get mypy to successfully pass at all. What can I do to get around this?

I should also say, I'd really rather not ignore all of b.py if possible. There's a decent amount of code in there (in reality) that would be good to type check.

0 投票
0 回答
800 浏览

python - 函数的类型注释,返回一个采用 args 和 kwargs 的函数

以下示例代码在运行时工作,但不被接受mypy --strict

mypy输出:

现在result*我不想给变量添加类型注释,我想调整函数的类型注释,这样就可以推导出其他的了。这是我的尝试:

但是现在参数的数量不再适合:

我想我正在寻找类似的东西

但是这种语法是不合法的。

有没有办法解决这个问题?

0 投票
1 回答
1680 浏览

python - How to inject class-variable annotations in Python 3.7+?

In Python 3.7, static fields can be annotated with the following syntax defined in PEP 526:

#xA;

How can I make these annotations later on, after the class is defined? I would expect the following to work:

#xA;

However, it doesn't seem to have an identical effect to the first code. When we look at A's __dict___, those two snippets don't have the same effect.

After example 1 and after example 2, we get identical __dict__'s, i.e. the second example must show effect somewhere else. The dict created is:

#xA;

I don't think editing __annotations__ is a "good" method of achieving what I want, especially because I'm not sure if it's the only case where foo is registered.

What is the proper way to do this?

0 投票
3 回答
7837 浏览

python - Python 3.7:检查类型注释是否是泛型的“子类”

我试图找到一种可靠/跨版本(3.5+)的方法来检查类型注释是否是给定泛型类型的“子类”(即从类型注释对象中获取泛型类型)。

在 Python 3.5 / 3.6 上,它可以轻而易举地工作,正如您所期望的:

在 3.7 上,看起来泛型类型的实例不再是 的实例type,所以它会失败:

想到的其他想法是检查实际的实例类型,但是:

Python 3.6 / 3.5:

蟒蛇 3.7:

但这并没有真正进一步表明哪个是实际的泛型类型(可能不是 List);此外,以这种方式进行检查感觉非常错误,特别是因为_GenericAliasnow 变成了“私有”类型(注意下划线)。

可以检查的另一件事是__origin__关于类型的参数,但这也不是正确的方法。

它在 3.7 上仍然不同:

而 3.5 / 3.6:

我一直在寻找这样做的“正确”方式,但在 Python 文档/谷歌搜索中没有找到它。

现在,我假设必须有一种干净的方式来进行这种检查,因为像 mypy 这样的工具会依赖它来进行类型检查..?

更新:关于用例

好的,在这里添加更多上下文..

因此,我的用例是对函数签名(参数类型/默认值、返回类型、文档字符串)使用自省来自动为它们生成 GraphQL 模式(从而减少样板的数量)。

对于这是否是一个好主意,我仍然有点担心。

从可用性的角度来看,我喜欢它(无需学习另一种声明函数签名的方法:只需以通常的方式注释您的类型);请参阅此处的两个代码示例以了解我的意思:https ://github.com/rshk/pyql

我想知道以这种方式使用类型支持泛型类型(列表、字典、联合......)是否typing会增加太多“黑魔法”,这可能会以意想不到的方式破坏。(目前这不是一个大问题,但是未来的 Python 版本,超过 3.7 版本呢?这会成为维护的噩梦吗?)。

当然,替代方法是只使用支持更可靠/面向未来的检查的自定义类型注释,例如:https://github.com/rshk/pyql/blob/master/pyql/schema/types/core。 py#L337-L339

..但不利的一面是,这将迫使人们记住他们必须使用自定义类型注释。此外,我不确定 mypy 将如何处理(我认为需要在某处声明自定义类型与typing.List.. 完全兼容?听起来仍然很骇人听闻)。

(我主要是在寻求有关这两种方法的建议,最重要的是我可能错过的两种选择的任何优点/缺点。希望这不会变得“太宽泛”。)。