반응형
pyright 사용하기 ][ static typing 테스트 해보기
■ 설치하기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
■ 테스트 소스
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ) |

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def (a: Optional[Union[str, List[str]]: | |
if isinstance(a, str): | |
log(a) | |
elif a: | |
log(msg) for msg in a | |
else: | |
log(a) |
■ 소스 수정시 자동 체크
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pyright -P /usr/bin/python3 test2.py -w |

■ 여러개의 파일 체크

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hacker@ubuntu:~/pyright/sample$ pyright -p project1_config.json |

반응형