본문 바로가기
Python/0x0C-pyright

pyright 사용하기 ][ static typing 테스트 해보기

by SpeeDr00t 2019. 3. 28.
반응형

pyright 사용하기 ][ static typing 테스트 해보기

■ 설치하기

sudo apt install nodejs
sudo apt install npm
sudo npm install -g npx
git clone https://github.com/Microsoft/pyright.git
cd pyright
sudo npm i -g pyright
sudo npm run install:all
sudo npm run build
sudo npm run package
hacker@ubuntu:~/pyright/sample$ pyright -h
Usage: pyright [options] files...
Options:
-h,--help Show this help message
-P,--python-path DIRECTORY Directory that contains the python environment
-p,--project FILE OR DIRECTORY Use the configuration file at this location
-t,--typeshed-path DIRECTORY Use typeshed type stubs at this location
-v,--venv-path DIRECTORY Directory that contains virtual environments
-w,--watch Continue to run and watch for changes
pyright -P /usr/bin/python3 test2.py -w

■ 테스트 소스

import itertools
from typing import Iterator
def iter_primes() -> Iterator[int]:
# An iterator of all numbers between 2 and
# +infinity
numbers = itertools.count(2)
# Generate primes forever
while True:
# Get the first number from the iterator
# (always a prime)
prime = next(numbers)
yield prime
# This code iteratively builds up a chain
# of filters...
numbers = filter(prime.__rmod__, numbers)
for p in iter_primes():
if p > 1000:
break
print(p)
view raw mypy1.py hosted with ❤ by GitHub

class CTest :
def __init__( self ) :
pass
def test1( self ) -> int :
return 1
def test2( self ) -> str :
return "hello world"
if __name__ == '__main__':
t = CTest()
print( t.test1() )
print( t.test2() )
view raw test2.py hosted with ❤ by GitHub

def (a: Optional[Union[str, List[str]]:
if isinstance(a, str):
log(a)
elif a:
log(msg) for msg in a
else:
log(a)

 

■ 소스 수정시 자동 체크

pyright -P /usr/bin/python3 test2.py -w
view raw test.sh hosted with ❤ by GitHub

소스 수정시 자동 검사

■ 여러개의 파일 체크 

tree 구조

{
"include": ["src"],
"exclude": ["doc"],
"typingsPath": "src/typestubs",
"reportTypeshedErrors": true,
"reportMissingImports": false,
"pythonVersion": "3.7",
"pythonPlatform": "Darwin",
"executionEnvironments": [
{
"root": "src/sdk",
"pythonVersion": "3.0",
"extraPaths": ["src/backend"]
},
{
"root": "src"
}
]
}
hacker@ubuntu:~/pyright/sample$ pyright -p project1_config.json
view raw package.sh hosted with ❤ by GitHub

반응형