天道酬勤,学无止境

static-typing

mypy not detecting a basic type error

问题 使用 python 3.5.1。 和当前使用 git 安装的 mypy,mypy 标记错误 1 ​​和 2,但它不报告 3 我做错了什么,或者这是一个错误,还是这是一个已知问题? import typing def test_ordered_dict(od: typing.Dict[str,int]) -> typing.Dict[str,int]: return 1 #type error 1 a = test_ordered_dict(1) #type error 2 def test_me(): a = test_ordered_dict(1) # type error 3 is not reported 回答1 我对文档的理解是 mypy 只会检查一个东西(模块、函数等),如果它指示它应该检查它(通过在模块级别导入类型或通过注释一个函数)。 所以检查 1 是因为它在一个类型化的函数中,检查 2 是因为导入类型表明你的模块是类型化的并且它在模块范围内,但是 3 在一个非类型化函数的范围内,所以它被忽略了。

2022-05-12 15:25:04    分类:技术分享    python   static-typing   mypy

How can I import an enum through an intermediate TS file?

I file A I have: export enum MyFluffyEnum { Beauty, Courage, Love } In file B I have: import { MyFluffyEnum } from "./A"; export type { MyFluffyEnum }; In file C I have: import { MyFluffyEnum } from "./B"; Compile-time error: 'MyFluffyEnum' cannot be used as a value because it was exported using 'export type'. If I put a separate export { MyFluffyEnum }; in the file B (the intermediate file) I get the error "Module not found: Can't resolve 'B.tsx' in 'my-project/path/here'". If I import the file with the enum as named export directly, then there is no error. Links that may be relevant: Export

2022-05-01 08:24:14    分类:问答    typescript   enums   static-typing

Type 'A | B' is not assignable to type 'A & B'

Why does this code not compile? type A = { n: number } type B = { s: string } type Thing = { a: A b: B } function update(obj: Thing, path: keyof Thing) { obj[path] = obj[path] } I would expect both sides of the assignment to have type A | B but the TypeScript compiler fails with: error TS2322: Type 'A | B' is not assignable to type 'A & B'. Type 'A' is not assignable to type 'A & B'. Property 's' is missing in type 'A' but required in type 'B'. 10 obj[path] = obj[path] ~~~~~~~~~ Is there a way to make it work?

2022-05-01 06:06:21    分类:问答    typescript   static-typing   union-types

mypy not detecting a basic type error

With python 3.5.1. and a current installation of mypy using git, mypy flags error 1 & 2, but it does not report 3 What am I doing wrong, or is this a bug, or is this a known problem? import typing def test_ordered_dict(od: typing.Dict[str,int]) -> typing.Dict[str,int]: return 1 #type error 1 a = test_ordered_dict(1) #type error 2 def test_me(): a = test_ordered_dict(1) # type error 3 is not reported

2022-04-30 10:51:46    分类:问答    python   static-typing   mypy

Should you put quotes around type annotations in python

What's the difference between these two functions? I've seen people put quotes around type annotations and other times leave them out but I couldn't find why people choose to use one or the other. def do_something(entity: Entity): pass def do_something(entity: 'Entity'): pass Are there advantages or disadvantages to any of these?

2022-04-29 00:59:19    分类:问答    python   python-3.x   type-hinting   static-typing

Is "Lisp-1 vs Lisp-2" relevant in a language with static types?

(This is a CS-theory type of question; I hope that's acceptable.) The "Lisp-1 vs Lisp-2" debate is about whether the namespace of functions should be distinct from the namespace of all other variables, and it's relevant in dynamically typed languages that allow the programmer to pass around functions as values. Lisp-1 languages (such as Scheme) have one namespace, so you can't have both a function named f and also an integer named f (one would shadow the other, just like two integers named f). Lisp-2 languages (such as Common Lisp) have two namespaces, so you can have both f variables, but you

2022-03-21 04:39:41    分类:问答    namespaces   lisp   static-typing   lisp-2

overload following optional argument

I have a class Animal with a method foo which has different return types according to a boolean parameter inplace which follows an optional parameter bar. I'd like to overload the function so that the return type is known if the value of inplace is known Here's my code: # main.py from __future__ import annotations from typing import Optional, overload, Literal class Animal: @overload def foo(self, bar=..., inplace: Literal[False]=...) -> Animal: ... @overload def foo(self, bar=..., inplace: Literal[True]=...) -> None: ... def foo( self, bar=None, inplace: bool = False ) -> Optional[Animal]:

2022-03-19 21:20:03    分类:问答    python   mypy   static-typing

How to make mypy complain about assigning an Any to an int (part 2)

(This is a follow-up to this question.) My code base is fully statically typed (annotation) but at some points there is the Any type, for example because a value was parsed from a JSON string. Here is my minimal example: import json from typing import Any, Dict, Union def main() -> None: data = json.loads('{"value" = "three"}') my_int: int = data['value'] if __name__ == "__main__": main() mypy --strict accepts this code. However I would like to find these places automatically, to take the appropriate security measures. Is there any possibility to make mypy complain about the my_int: int = data

2022-03-19 07:12:30    分类:问答    python-3.x   type-hinting   mypy   typechecking   static-typing

Common Lisp type declarations not working as expected

When I define a function in Common Lisp like this: (defun foo (n) (declare (type fixnum n)) (+ n 42)) I expected a call like (foo "a") to fail right away but it instead fail at the call to +. Is the declare form not guarantees static type checking?

2022-03-18 13:29:42    分类:问答    lisp   common-lisp   static-typing