Skip to content
This repository has been archived by the owner on Feb 4, 2020. It is now read-only.

Commit

Permalink
Enable static code analysis via mypy
Browse files Browse the repository at this point in the history
This adds a few type annotations to the source code and
enables running mypy as part of the CI build.
  • Loading branch information
xoviat authored and frerich committed Nov 1, 2017
1 parent 05b2931 commit 84eafec
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
5 changes: 5 additions & 0 deletions appveyor.yml
Expand Up @@ -38,6 +38,9 @@ install:

# Install pytest
- pip install pytest

# Install mypy
- pip install mypy

# Install pylint (installs pylint.exe in %PYTHON_INSTALL%\Scripts)
- pip install pylint
Expand Down Expand Up @@ -75,6 +78,8 @@ build_script:

# Disable no-member test here to work around issue in Pylint 1.7.1
- pylint --rcfile=.pylintrc --disable=no-member clcache\server\__main__.py

- mypy --ignore-missing-imports .

test_script:
# Run test files via py.test and generate JUnit XML. Then push test results
Expand Down
16 changes: 7 additions & 9 deletions clcache/__main__.py
Expand Up @@ -25,6 +25,7 @@
import sys
import threading
from tempfile import TemporaryFile
from typing import Any, List, Tuple, Iterator

VERSION = "4.1.0-dev"

Expand Down Expand Up @@ -524,7 +525,7 @@ def __init__(self, cacheDirectory=None):
def __str__(self):
return "Disk cache at {}".format(self.dir)

@property
@property # type: ignore
@contextlib.contextmanager
def lock(self):
with allSectionsLocked(self.manifestRepository), \
Expand Down Expand Up @@ -1016,8 +1017,7 @@ def findCompilerBinary():
return None


def printTraceStatement(msg):
# type: (str) -> None
def printTraceStatement(msg: str) -> None:
if "CLCACHE_LOG" in os.environ:
scriptDir = os.path.realpath(os.path.dirname(sys.argv[0]))
with OUTPUT_LOCK:
Expand Down Expand Up @@ -1265,8 +1265,7 @@ def parseArgumentsAndInputFiles(cmdline):
return dict(arguments), inputFiles

@staticmethod
def analyze(cmdline):
# type: List[str] -> Tuple[List[Tuple[str, str]], List[str]]
def analyze(cmdline: List[str]) -> Tuple[List[Tuple[str, str]], List[str]]:
options, inputFiles = CommandLineAnalyzer.parseArgumentsAndInputFiles(cmdline)
# Use an override pattern to shadow input files that have
# already been specified in the function above
Expand Down Expand Up @@ -1637,17 +1636,16 @@ def processCompileRequest(cache, compiler, args):
printOutAndErr(out, err)
return exitCode

def filterSourceFiles(cmdLine, sourceFiles):
# type: (List[str], List[Tuple[str, str]]) -> Iterator[str]
def filterSourceFiles(cmdLine: List[str], sourceFiles: List[Tuple[str, str]]) -> Iterator[str]:
setOfSources = set(sourceFile for sourceFile, _ in sourceFiles)
skippedArgs = ('/Tc', '/Tp', '-Tp', '-Tc')
yield from (
arg for arg in cmdLine
if not (arg in setOfSources or arg.startswith(skippedArgs))
)

def scheduleJobs(cache, compiler, cmdLine, environment, sourceFiles, objectFiles):
# type: (Any, str, List[str], Any, List[Tuple[str, str]], List[str]) -> int
def scheduleJobs(cache: Any, compiler: str, cmdLine: List[str], environment: Any,
sourceFiles: List[Tuple[str, str]], objectFiles: List[str]) -> int:
# Filter out all source files from the command line to form baseCmdLine
baseCmdLine = [arg for arg in filterSourceFiles(cmdLine, sourceFiles) if not arg.startswith('/MP')]

Expand Down
2 changes: 1 addition & 1 deletion clcache/storage.py
Expand Up @@ -217,7 +217,7 @@ def lockFor(_):
def manifestLockFor(_):
return CacheDummyLock()

@property
@property # type: ignore
@contextlib.contextmanager
def lock(self):
with self.remoteCache.lock, self.localCache.lock:
Expand Down

0 comments on commit 84eafec

Please sign in to comment.