diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..bffe9b83e --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,45 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: check-yaml + - id: name-tests-test + args: [--unittest] + exclude: ^tests/utils.py +- repo: https://github.com/pycqa/isort + rev: 5.12.0 + hooks: + - id: isort + args: [--profile=black, --gitignore] +- repo: https://github.com/psf/black + rev: 23.10.1 + hooks: + - id: black + args: [--line-length=127] +- repo: https://github.com/PyCQA/flake8 + rev: 6.1.0 + hooks: + - id: flake8 + args: + - --count + - --max-complexity=55 + - --max-line-length=127 + - --statistics + - --extend-ignore=E203,E231,E252,E721,F403,F405,F541,W503 + files: zappa +- repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.6.1 + hooks: + - id: mypy + args: + - --show-error-codes + - --pretty + - --ignore-missing-imports + - --no-site-packages + files: zappa +- repo: https://github.com/thlorenz/doctoc + rev: v2.2.0 + hooks: + - id: doctoc + args: [--update-only] + files: ./README.md diff --git a/Pipfile b/Pipfile index f8d05fbd9..89ab19269 100644 --- a/Pipfile +++ b/Pipfile @@ -15,6 +15,7 @@ isort = "*" mock = "*" mypy = "*" packaging = "*" +pre-commit = "*" pytest = "*" pytest-cov = "*" diff --git a/README.md b/README.md index 6756d0b7a..d386b6fef 100644 --- a/README.md +++ b/README.md @@ -1196,14 +1196,13 @@ If you want to use native AWS Lambda environment variables you can use the `aws_ During development, you can add your Zappa defined variables to your locally running app by, for example, using the below (for Django, to manage.py). ```python -if 'SERVERTYPE' in os.environ and os.environ['SERVERTYPE'] == 'AWS Lambda': - import json - import os +import json +import os + +if os.environ.get('AWS_LAMBDA_FUNCTION_NAME') is None: # Ensures app is NOT running on Lambda json_data = open('zappa_settings.json') env_vars = json.load(json_data)['dev']['environment_variables'] - for key, val in env_vars.items(): - os.environ[key] = val - + os.environ.update(env_vars) ``` #### Remote Environment Variables diff --git a/zappa/cli.py b/zappa/cli.py index 3fd7354ad..08e428056 100755 --- a/zappa/cli.py +++ b/zappa/cli.py @@ -120,7 +120,7 @@ class ZappaCLI: aws_kms_key_arn = "" context_header_mappings = None additional_text_mimetypes = None - tags = [] + tags = [] # type: ignore[var-annotated] layers = None stage_name_env_pattern = re.compile("^[a-zA-Z0-9_]+$") @@ -475,7 +475,7 @@ def positive_int(s): else: self.stage_env = self.vargs.get("stage_env") - if args.command == "package": + if args.command in ("package", "save-python-settings-file"): self.load_credentials = False self.command = args.command @@ -2415,14 +2415,14 @@ def create_package(self, output=None, use_zappa_release: Optional[str] = None): # Create the Lambda zip package (includes project and virtualenvironment) # Also define the path the handler file so it can be copied to the zip # root for Lambda. - current_file = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + current_file = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # type: ignore[arg-type] handler_file = os.sep.join(current_file.split(os.sep)[0:]) + os.sep + "handler.py" # Create the zip file(s) if self.stage_config.get("slim_handler", False): # Create two zips. One with the application and the other with just the handler. # https://github.com/Miserlou/Zappa/issues/510 - self.zip_path = self.zappa.create_lambda_zip( + self.zip_path = self.zappa.create_lambda_zip( # type: ignore[attr-defined] prefix=self.lambda_name, use_precompiled_packages=self.stage_config.get("use_precompiled_packages", True), exclude=self.stage_config.get("exclude", []), @@ -2433,11 +2433,11 @@ def create_package(self, output=None, use_zappa_release: Optional[str] = None): # Make sure the normal venv is not included in the handler's zip exclude = self.stage_config.get("exclude", []) - cur_venv = self.zappa.get_current_venv() + cur_venv = self.zappa.get_current_venv() # type: ignore[attr-defined] exclude.append(cur_venv.split("/")[-1]) - self.handler_path = self.zappa.create_lambda_zip( + self.handler_path = self.zappa.create_lambda_zip( # type: ignore[attr-defined] prefix="handler_{0!s}".format(self.lambda_name), - venv=self.zappa.create_handler_venv(use_zappa_release=use_zappa_release), + venv=self.zappa.create_handler_venv(use_zappa_release=use_zappa_release), # type: ignore[attr-defined] handler_file=handler_file, slim_handler=True, exclude=exclude, @@ -2449,7 +2449,7 @@ def create_package(self, output=None, use_zappa_release: Optional[str] = None): exclude = self.stage_config.get("exclude", []) # Create a single zip that has the handler and application - self.zip_path = self.zappa.create_lambda_zip( + self.zip_path = self.zappa.create_lambda_zip( # type: ignore[attr-defined] prefix=self.lambda_name, handler_file=handler_file, use_precompiled_packages=self.stage_config.get("use_precompiled_packages", True), @@ -2473,7 +2473,7 @@ def create_package(self, output=None, use_zappa_release: Optional[str] = None): else: handler_zip = self.zip_path - with zipfile.ZipFile(handler_zip, "a") as lambda_zip: + with zipfile.ZipFile(handler_zip, "a") as lambda_zip: # type: ignore[call-overload] settings_s = self.get_zappa_settings_string() # Copy our Django app into root of our package. diff --git a/zappa/handler.py b/zappa/handler.py index c1a7ff5e6..7287e09f7 100644 --- a/zappa/handler.py +++ b/zappa/handler.py @@ -30,7 +30,7 @@ # Set up logging logging.basicConfig() -logger = logging.getLogger() +logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) diff --git a/zappa/wsgi.py b/zappa/wsgi.py index 9f3051f04..f40407f62 100644 --- a/zappa/wsgi.py +++ b/zappa/wsgi.py @@ -168,7 +168,7 @@ def common_log(environ, response, response_time: Optional[int] = None): response_time: response time in micro-seconds """ - logger = logging.getLogger() + logger = logging.getLogger(__name__) if response_time: formatter = ApacheNCSAFormatter(with_response_time=True)