Skip to content

Commit

Permalink
Merge pull request #181 from asottile/pre-commit-ci-update-config
Browse files Browse the repository at this point in the history
[pre-commit.ci] pre-commit autoupdate
  • Loading branch information
asottile authored Jul 23, 2024
2 parents 23a24c6 + bcf2108 commit 86f0351
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ repos:
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.10.1
rev: v1.11.0
hooks:
- id: mypy
18 changes: 11 additions & 7 deletions dead.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing import Generator
from typing import NewType
from typing import Pattern
from typing import Protocol
from typing import Sequence
from typing import Set
from typing import Union
Expand All @@ -38,6 +39,11 @@
STUB_EXCEPTIONS = frozenset(('AssertionError', 'NotImplementedError'))


class _HasLineno(Protocol):
@property
def lineno(self) -> int: ...


class Scope:
def __init__(self) -> None:
self.reads: UsageMap = collections.defaultdict(set)
Expand Down Expand Up @@ -79,14 +85,14 @@ def scope(self) -> Generator[None, None, None]:
def _file_line(self, filename: str, line: int) -> FileLine:
return FileLine(f'{filename}:{line}')

def definition_str(self, node: ast.AST) -> FileLine:
def definition_str(self, node: _HasLineno) -> FileLine:
return self._file_line(self.filename, node.lineno)

def define(self, name: str, node: ast.AST) -> None:
def define(self, name: str, node: _HasLineno) -> None:
if not self.is_test:
self.scopes[-1].defines[name].add(self.definition_str(node))

def read(self, name: str, node: ast.AST) -> None:
def read(self, name: str, node: _HasLineno) -> None:
for scope in self.scopes:
if self.is_test:
scope.reads_tests[name].add(self.definition_str(node))
Expand Down Expand Up @@ -204,10 +210,8 @@ def visit_comment(self, lineno: int, line: str) -> None:

for part in parts:
ast_obj = ast.parse(part, f'<{self.filename}:{lineno}: comment>')
# adjust the line number to be that of the comment
for descendant in ast.walk(ast_obj):
if 'lineno' in descendant._attributes:
descendant.lineno = lineno
ast_obj.body[0].lineno = lineno
ast.fix_missing_locations(ast_obj)

self.visit(ast_obj)

Expand Down

0 comments on commit 86f0351

Please sign in to comment.