Skip to content

fix: modify type check to accept Callable (#317) #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:

steps:
- name: Harden Runner
uses: step-security/harden-runner@eb238b55efaa70779f274895e782ed17c84f2895 # v2.6.1
uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
with:
disable-sudo: true
egress-policy: block
Expand All @@ -57,7 +57,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@0b21cf2492b6b02c465a3e5d7c473717ad7721ba # v3.23.1
uses: github/codeql-action/init@379614612a29c9e28f31f39a59013eb8012a51f0 # v3.24.3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -67,7 +67,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@0b21cf2492b6b02c465a3e5d7c473717ad7721ba # v3.23.1
uses: github/codeql-action/autobuild@379614612a29c9e28f31f39a59013eb8012a51f0 # v3.24.3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
Expand All @@ -80,6 +80,6 @@ jobs:
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@0b21cf2492b6b02c465a3e5d7c473717ad7721ba # v3.23.1
uses: github/codeql-action/analyze@379614612a29c9e28f31f39a59013eb8012a51f0 # v3.24.3
with:
category: "/language:${{matrix.language}}"
2 changes: 1 addition & 1 deletion .github/workflows/conformance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
python: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12']
steps:
- name: Harden Runner
uses: step-security/harden-runner@eb238b55efaa70779f274895e782ed17c84f2895 # v2.6.1
uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
with:
disable-sudo: true
egress-policy: block
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@eb238b55efaa70779f274895e782ed17c84f2895 # v2.6.1
uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
with:
disable-sudo: true
egress-policy: block
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@eb238b55efaa70779f274895e782ed17c84f2895 # v2.6.1
uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
with:
disable-sudo: true
egress-policy: block
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@eb238b55efaa70779f274895e782ed17c84f2895 # v2.6.1
uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
with:
egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/scorecard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

steps:
- name: Harden Runner
uses: step-security/harden-runner@eb238b55efaa70779f274895e782ed17c84f2895 # v2.6.1
uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
with:
disable-sudo: true
egress-policy: block
Expand Down Expand Up @@ -61,6 +61,6 @@ jobs:

# Upload the results to GitHub's code scanning dashboard.
- name: "Upload to code-scanning"
uses: github/codeql-action/upload-sarif@0b21cf2492b6b02c465a3e5d7c473717ad7721ba # v3.23.1
uses: github/codeql-action/upload-sarif@379614612a29c9e28f31f39a59013eb8012a51f0 # v3.24.3
with:
sarif_file: results.sarif
2 changes: 1 addition & 1 deletion .github/workflows/unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
runs-on: ${{ matrix.platform }}
steps:
- name: Harden Runner
uses: step-security/harden-runner@eb238b55efaa70779f274895e782ed17c84f2895 # v2.6.1
uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
with:
disable-sudo: true
egress-policy: block
Expand Down
12 changes: 6 additions & 6 deletions src/functions_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def write(self, out):

def cloud_event(func: CloudEventFunction) -> CloudEventFunction:
"""Decorator that registers cloudevent as user function signature type."""
_function_registry.REGISTRY_MAP[
func.__name__
] = _function_registry.CLOUDEVENT_SIGNATURE_TYPE
_function_registry.REGISTRY_MAP[func.__name__] = (
_function_registry.CLOUDEVENT_SIGNATURE_TYPE
)

@functools.wraps(func)
def wrapper(*args, **kwargs):
Expand Down Expand Up @@ -105,9 +105,9 @@ def wrapper(*args, **kwargs):

def http(func: HTTPFunction) -> HTTPFunction:
"""Decorator that registers http as user function signature type."""
_function_registry.REGISTRY_MAP[
func.__name__
] = _function_registry.HTTP_SIGNATURE_TYPE
_function_registry.REGISTRY_MAP[func.__name__] = (
_function_registry.HTTP_SIGNATURE_TYPE
)

@functools.wraps(func)
def wrapper(*args, **kwargs):
Expand Down
8 changes: 4 additions & 4 deletions src/functions_framework/_function_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import types

from re import T
from typing import Type
from typing import Type, Callable

from functions_framework.exceptions import (
InvalidConfigurationException,
Expand Down Expand Up @@ -57,11 +57,11 @@ def get_user_function(source, source_module, target):
)
)
function = getattr(source_module, target)
# Check that it is a function
if not isinstance(function, types.FunctionType):
# Check that it is callable.
if not isinstance(function, Callable):
raise InvalidTargetTypeException(
"The function defined in file {source} as '{target}' needs to be of "
"type function. Got: invalid type {target_type}".format(
"type Callable. Got: invalid type {target_type}".format(
source=source, target=target, target_type=type(function)
)
)
Expand Down
7 changes: 4 additions & 3 deletions src/functions_framework/_http/gunicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ class GunicornApplication(gunicorn.app.base.BaseApplication):
def __init__(self, app, host, port, debug, **options):
self.options = {
"bind": "%s:%s" % (host, port),
"workers": 1,
"threads": (os.cpu_count() or 1) * 4,
"timeout": 0,
"workers": os.environ.get("WORKERS", (os.cpu_count() or 1) * 4),
"threads": os.environ.get("THREADS", 1),
"timeout": os.environ.get("CLOUD_RUN_TIMEOUT_SECONDS", 300),
"loglevel": "error",
"limit_request_line": 0,
}
self.options.update(options)
self.app = app

super().__init__()

def load_config(self):
Expand Down
6 changes: 3 additions & 3 deletions src/functions_framework/_typed_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def register_typed_event(decorator_type, func):
)

_function_registry.INPUT_TYPE_MAP[func.__name__] = input_type
_function_registry.REGISTRY_MAP[
func.__name__
] = _function_registry.TYPED_SIGNATURE_TYPE
_function_registry.REGISTRY_MAP[func.__name__] = (
_function_registry.TYPED_SIGNATURE_TYPE
)


""" Checks whether the response type of the typed function has a to_dict method"""
Expand Down
12 changes: 6 additions & 6 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ def test_gunicorn_application(debug):
assert gunicorn_app.app == app
assert gunicorn_app.options == {
"bind": "%s:%s" % (host, port),
"workers": 1,
"threads": os.cpu_count() * 4,
"timeout": 0,
"workers": os.cpu_count() * 4,
"threads": 1,
"timeout": 300,
"loglevel": "error",
"limit_request_line": 0,
}

assert gunicorn_app.cfg.bind == ["1.2.3.4:1234"]
assert gunicorn_app.cfg.workers == 1
assert gunicorn_app.cfg.threads == os.cpu_count() * 4
assert gunicorn_app.cfg.timeout == 0
assert gunicorn_app.cfg.workers == os.cpu_count() * 4
assert gunicorn_app.cfg.threads == 1
assert gunicorn_app.cfg.timeout == 300
assert gunicorn_app.load() == app


Expand Down