typechecking

Pycharm type checker - expected Series, got int

≡放荡痞女 提交于 2021-02-19 16:35:38
问题 I have very simple Python function that subtracts scalar value from pandas Series: def sub(x: pd.Series, a: int) -> pd.Series: return x - a It works as expected. I'm using type hints to enable type checker directly in my PyCharm IDE. The issue here is I get this warning message: Expected type 'Series', got 'int' instead As you can see in the image below: I understand that Python is dynamically typed language so in some cases type checking with type hints has its own limitations. But this

Python: why use overloading instead of *args in a function (especially when the type of arguments do not affect how the function runs)

余生长醉 提交于 2021-02-11 06:52:45
问题 Edited: I was looking at the type annotations of the built-in zip() function. I understand that overload (in the context of type checking), can modify the behaviour of a function depending on the type of parameters it's given. @overload def zip(__iter1: Iterable[_T1]) -> List[Tuple[_T1]]: ... @overload def zip(__iter1: Iterable[_T1], __iter2: Iterable[_T2]) -> List[Tuple[_T1, _T2]]: ... @overload def zip(__iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3]) -> List[Tuple[_T1

Python: why use overloading instead of *args in a function (especially when the type of arguments do not affect how the function runs)

雨燕双飞 提交于 2021-02-11 06:52:16
问题 Edited: I was looking at the type annotations of the built-in zip() function. I understand that overload (in the context of type checking), can modify the behaviour of a function depending on the type of parameters it's given. @overload def zip(__iter1: Iterable[_T1]) -> List[Tuple[_T1]]: ... @overload def zip(__iter1: Iterable[_T1], __iter2: Iterable[_T2]) -> List[Tuple[_T1, _T2]]: ... @overload def zip(__iter1: Iterable[_T1], __iter2: Iterable[_T2], __iter3: Iterable[_T3]) -> List[Tuple[_T1

'thisArg' context not correctly recognized in bound function with TypeScript

痞子三分冷 提交于 2021-01-29 22:12:23
问题 With the below code, TypeScript won't recognize the context of thisArg (which should be a and should have saySomething() method. is there another way to give context of thisArg for TypeScript? The code: class A { saySomething() { console.log('Hello!'); } } class B { constructor(a: A) { const boundSaySomething = this.saySomethingMyself.bind(a); boundSaySomething(); } saySomethingMyself() { this.saySomething(); } } const test = new B(new A()); the console correctly logs Hello , but the the type

'thisArg' context not correctly recognized in bound function with TypeScript

只愿长相守 提交于 2021-01-29 20:57:03
问题 With the below code, TypeScript won't recognize the context of thisArg (which should be a and should have saySomething() method. is there another way to give context of thisArg for TypeScript? The code: class A { saySomething() { console.log('Hello!'); } } class B { constructor(a: A) { const boundSaySomething = this.saySomethingMyself.bind(a); boundSaySomething(); } saySomethingMyself() { this.saySomething(); } } const test = new B(new A()); the console correctly logs Hello , but the the type

Do type annotations in Python enforce static type checking?

杀马特。学长 韩版系。学妹 提交于 2021-01-29 10:33:07
问题 I'm trying to enforce some static type checking in a project written in Python, using typing module. When I define a function like the one from the doc def greeting(name: str) -> str: return 'Hello ' + name and try to do something like greeting(3) , I indeed got the following error Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in greeting TypeError: must be str, not int But when I again define a function called test def test(a: int) -> None:

Size of a box containing a struct with a trait parameter

半世苍凉 提交于 2021-01-04 06:53:34
问题 I need a struct that contains a trait object and more of itself. Disappointedly the following code does not compile: trait Foo {} struct Bar<T: Foo> { bars: Vec<Box<Bar<dyn Foo>>>, foo: T, } I managed to coerce this into compiling by adding the ?Sized bound to T , but I do not understand why this should be the case. I assume this is because all trait objects have the same size, but the size of Bar depends on the size of the concrete type T . If so, how is Bar with an unsized T represented in

What cases do the GHC occurs check identify?

ⅰ亾dé卋堺 提交于 2021-01-02 07:09:21
问题 The GHC occurs check prevents you from constructing infinite types. Is its purpose to prevent common errors in code or to prevent the typechecker from looping indefinitely, or both? What cases does it identify and is it possible for a malicious user to trick it (as in a Safe Haskell context) into looping? If the type system is Turing-complete (is it?) I don't understand how GHC can guarantee that the computation will halt. 回答1: Think of type inference as solving a system of equations. Let's

What cases do the GHC occurs check identify?

时光总嘲笑我的痴心妄想 提交于 2021-01-02 07:08:16
问题 The GHC occurs check prevents you from constructing infinite types. Is its purpose to prevent common errors in code or to prevent the typechecker from looping indefinitely, or both? What cases does it identify and is it possible for a malicious user to trick it (as in a Safe Haskell context) into looping? If the type system is Turing-complete (is it?) I don't understand how GHC can guarantee that the computation will halt. 回答1: Think of type inference as solving a system of equations. Let's

ESLint not reporting TypeScript compiler type checking errors

一笑奈何 提交于 2020-12-25 04:03:24
问题 Looking for help in getting the type errors, reported by the TypeScript compiler, into the output of ESLint. The library typescript-eslint (https://github.com/typescript-eslint/typescript-eslint/blob/master/docs/getting-started/linting/TYPED_LINTING.md) makes me think that this should be possible. File structure src/ ... source files tsconfig.json test/ ... testing files .eslintrc.js package.json tsconfig.json (symlink to src/tsconfig.json) .eslintrc.js module.exports = { 'env': { 'jest':