Skip to content

Instantly share code, notes, and snippets.

@asford
Last active April 24, 2021 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asford/cd3c8ebbf245df6c3666eec0852c171d to your computer and use it in GitHub Desktop.
Save asford/cd3c8ebbf245df6c3666eec0852c171d to your computer and use it in GitHub Desktop.
micromamba
.conda
node_modules
from attr import attrib, field, define
def custom_define(*args, **kwargs):
return attr.define(*args, **kwargs)
from typing import Any, Callable, Tuple, TypeVar, Union
from attr import attrib, define, field
_T = TypeVar("_T")
def __dataclass_transform__(
*,
eq_default: bool = True,
order_default: bool = False,
kw_only_default: bool = False,
field_descriptors: Tuple[Union[type, Callable[..., Any]], ...] = (()),
) -> Callable[[_T], _T]:
...
@__dataclass_transform__(field_descriptors=(attrib, field))
def custom_define(*args, **kwargs):
...
channels:
- conda-forge
dependencies:
- attrs
- python=3.9
- nodejs
- mypy=0.812
.conda/bin/mypy
.conda/bin/npm
{
"name": "cd3c8ebbf245df6c3666eec0852c171d",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"dependencies": {
"pyright": "^1.1.134"
}
},
"node_modules/pyright": {
"version": "1.1.134",
"resolved": "https://registry.npmjs.org/pyright/-/pyright-1.1.134.tgz",
"integrity": "sha512-wQSdU6X3olAwCZy3tSA0fn8nMQGEwm01rm1dHM+aN2crzXIcUQ9sLOf+wCn5PFlLGsm/CXH7ROYmeMs3jXQ8Rw==",
"bin": {
"pyright": "index.js",
"pyright-langserver": "langserver.index.js"
},
"engines": {
"node": ">=12.0.0"
}
}
},
"dependencies": {
"pyright": {
"version": "1.1.134",
"resolved": "https://registry.npmjs.org/pyright/-/pyright-1.1.134.tgz",
"integrity": "sha512-wQSdU6X3olAwCZy3tSA0fn8nMQGEwm01rm1dHM+aN2crzXIcUQ9sLOf+wCn5PFlLGsm/CXH7ROYmeMs3jXQ8Rw=="
}
}
}
{
"dependencies": {
"pyright": "^1.1.134"
}
}
node_modules/pyright/index.js

Expected

Demo of microsoft/pyright#1782 with custom wrapper. Adding __dataclass_transform__ in .pyi.

Expected output:

+ ./mypy test.py
test.py:9: note: Revealed type is 'def (self: builtins.object)'
+ ./pyright test.py
No configuration file found.
No pyproject.toml file found.
stubPath /home/alexford/pyright_test_attrs/typings is not a valid directory.
Assuming Python platform Linux
Searching for source files
Found 1 source file
/home/alexford/pyright_test_attrs/test.py
  /home/alexford/pyright_test_attrs/test.py:9:13 - info: Type of "Data.__init__" is "(self: Data, a: str, b: int) -> None"
0 errors, 0 warnings, 1 info
Completed in 0.786sec
#!/usr/bin/env python
import sys
import subprocess
def check_call(cmd):
print("+ " + cmd)
subprocess.check_call(cmd, shell=True)
def call(cmd):
print("+ " + cmd)
subprocess.call(cmd, shell=True)
url = {
"linux": "https://micromamba.snakepit.net/api/micromamba/linux-64/latest",
"darwin": "https://micromamba.snakepit.net/api/micromamba/osx-64/latest"
}[sys.platform]
check_call("wget -qO- " + url + " | tar -xvj bin/micromamba")
check_call("mv bin/micromamba micromamba")
check_call("rmdir bin")
check_call("./micromamba create --prefix ./.conda --file environment.yml")
check_call("./npm install pyright")
call("./mypy test.py")
call("./pyright test.py")
import attr
from custom import custom_define
@custom_define
class Data:
a: str
b: int = attr.field()
reveal_type(Data.__init__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment