Fix memoize type issues and enable mypy in lint script (#1518)

* memoize type fixes

* Add mypy to lint script

* Add mypy to dev-requirements.txt
pull/1519/head
Gulshan Singh 3 years ago committed by GitHub
parent 301d0b9ccd
commit bc59a8eddd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,8 +6,7 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
# Only care about the oldest and newest versions os: [ubuntu-22.04, ubuntu-20.04, ubuntu-18.04]
os: [ubuntu-22.04, ubuntu-18.04]
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
timeout-minutes: 10 timeout-minutes: 10
steps: steps:

@ -6,10 +6,14 @@ flake8==5.0.4; python_version < '3.9'
flake8==6.0.0; python_version >= '3.9' flake8==6.0.0; python_version >= '3.9'
flake8-builtins==2.0.0; python_version < '3.7' flake8-builtins==2.0.0; python_version < '3.7'
flake8-builtins==2.1.0; python_version >= '3.7' flake8-builtins==2.1.0; python_version >= '3.7'
mypy==0.991; python_version >= '3.7'
testresources==2.0.1 testresources==2.0.1
isort==5.10.1; python_version < '3.7' isort==5.10.1; python_version < '3.7'
isort==5.11.4; python_version >= '3.7' isort==5.11.4; python_version >= '3.7'
pytest==7.0.1; python_version < '3.7' pytest==7.0.1; python_version < '3.7'
pytest==7.2.0; python_version >= '3.7' pytest==7.2.0; python_version >= '3.7'
types-gdb==12.1.4.1 types-gdb==12.1.4.1
types-psutil==5.9.5
types-Pygments==2.12.1
types-tabulate==0.9.0.0
vermin==1.5.1 vermin==1.5.1

@ -59,3 +59,9 @@ fi
vermin -q -t=3.6 --violations ./pwndbg/ vermin -q -t=3.6 --violations ./pwndbg/
flake8 --show-source ${LINT_FILES} flake8 --show-source ${LINT_FILES}
if [ -x "$(command -v mypy)" ]; then
mypy pwndbg
else
echo "mypy not installed, skipping"
fi

@ -6,6 +6,7 @@ new library/objfile are loaded, etc.
import functools import functools
import sys import sys
import typing as t
from collections.abc import Hashable from collections.abc import Hashable
from typing import Any from typing import Any
from typing import Callable from typing import Callable
@ -14,6 +15,17 @@ from typing import List # noqa: F401
debug = False debug = False
# https://stackoverflow.com/a/75013308/803801
if t.TYPE_CHECKING:
F = t.TypeVar("F")
reset_on_stop: Callable[[F], F]
reset_on_prompt: Callable[[F], F]
reset_on_exit: Callable[[F], F]
reset_on_objfile: Callable[[F], F]
reset_on_start: Callable[[F], F]
reset_on_cont: Callable[[F], F]
reset_on_thread: Callable[[F], F]
else:
class memoize: class memoize:
""" """
@ -65,7 +77,6 @@ class memoize:
print("Clearing %s %r" % (self, self.cache)) print("Clearing %s %r" % (self, self.cache))
self.cache.clear() self.cache.clear()
class forever(memoize): class forever(memoize):
""" """
Memoizes forever - for a pwndbg session or until `_reset` is called explicitly. Memoizes forever - for a pwndbg session or until `_reset` is called explicitly.
@ -79,7 +90,6 @@ class forever(memoize):
for obj in forever.caches: for obj in forever.caches:
obj.cache.clear() obj.cache.clear()
class reset_on_stop(memoize): class reset_on_stop(memoize):
caches = [] # type: List[reset_on_stop] caches = [] # type: List[reset_on_stop]
kind = "stop" kind = "stop"
@ -91,7 +101,6 @@ class reset_on_stop(memoize):
_reset = __reset_on_stop _reset = __reset_on_stop
class reset_on_prompt(memoize): class reset_on_prompt(memoize):
caches = [] # type: List[reset_on_prompt] caches = [] # type: List[reset_on_prompt]
kind = "prompt" kind = "prompt"
@ -103,7 +112,6 @@ class reset_on_prompt(memoize):
_reset = __reset_on_prompt _reset = __reset_on_prompt
class reset_on_exit(memoize): class reset_on_exit(memoize):
caches = [] # type: List[reset_on_exit] caches = [] # type: List[reset_on_exit]
kind = "exit" kind = "exit"
@ -115,7 +123,6 @@ class reset_on_exit(memoize):
_reset = __reset_on_exit _reset = __reset_on_exit
class reset_on_objfile(memoize): class reset_on_objfile(memoize):
caches = [] # type: List[reset_on_objfile] caches = [] # type: List[reset_on_objfile]
kind = "objfile" kind = "objfile"
@ -127,7 +134,6 @@ class reset_on_objfile(memoize):
_reset = __reset_on_objfile _reset = __reset_on_objfile
class reset_on_start(memoize): class reset_on_start(memoize):
caches = [] # type: List[reset_on_start] caches = [] # type: List[reset_on_start]
kind = "start" kind = "start"
@ -139,7 +145,6 @@ class reset_on_start(memoize):
_reset = __reset_on_start _reset = __reset_on_start
class reset_on_cont(memoize): class reset_on_cont(memoize):
caches = [] # type: List[reset_on_cont] caches = [] # type: List[reset_on_cont]
kind = "cont" kind = "cont"
@ -151,7 +156,6 @@ class reset_on_cont(memoize):
_reset = __reset_on_cont _reset = __reset_on_cont
class reset_on_thread(memoize): class reset_on_thread(memoize):
caches = [] # type: List[reset_on_thread] caches = [] # type: List[reset_on_thread]
kind = "thread" kind = "thread"
@ -163,7 +167,6 @@ class reset_on_thread(memoize):
_reset = __reset_on_thread _reset = __reset_on_thread
class while_running(memoize): class while_running(memoize):
caches = [] # type: List[while_running] caches = [] # type: List[while_running]
kind = "running" kind = "running"
@ -181,7 +184,6 @@ class while_running(memoize):
_reset = __reset_while_running _reset = __reset_while_running
def reset() -> None: def reset() -> None:
forever._reset() forever._reset()
reset_on_stop._reset() reset_on_stop._reset()

@ -4,11 +4,13 @@ which prevent output from appearing on-screen inside of certain event handlers.
""" """
import sys import sys
from typing import * # noqa note: TextIO is not abaliable in low python version from typing import List
from typing import TextIO
from typing import Tuple
class Stdio: class Stdio:
queue = [] # type: List[Tuple[TextIO, TextIO, TextIO]] queue: List[Tuple[TextIO, TextIO, TextIO]] = []
def __enter__(self, *a, **kw) -> None: def __enter__(self, *a, **kw) -> None:
self.queue.append((sys.stdin, sys.stdout, sys.stderr)) self.queue.append((sys.stdin, sys.stdout, sys.stderr))

@ -20,9 +20,7 @@ disable_error_code = [
# Lots of dynamic attribute access # Lots of dynamic attribute access
"attr-defined", "attr-defined",
# https://github.com/python/mypy/issues/6232 # https://github.com/python/mypy/issues/6232
"assignment", "assignment"
# Issues with @property
"operator",
] ]
[[tool.mypy.overrides]] [[tool.mypy.overrides]]

Loading…
Cancel
Save