Skip to content

Commit 91afba2

Browse files
authored
Load the source file into the correct module name (#50)
* Add a failing test * Load the source file into the correct module name
1 parent 2337104 commit 91afba2

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Fixed
9+
- Load the source file into the correct module name ([#49])
810

911
## [1.4.2] - 2020-05-13
1012
### Fixed
@@ -66,6 +68,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6668
[1.0.1]: https://github.com/GoogleCloudPlatform/functions-framework-python/releases/tag/v1.0.1
6769
[1.0.0]: https://github.com/GoogleCloudPlatform/functions-framework-python/releases/tag/v1.0.0
6870

71+
[#49]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/49
6972
[#44]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/44
7073
[#38]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/38
7174
[#33]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/33

src/functions_framework/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def create_app(target=None, source=None, signature_type=None):
141141
os.environ["FUNCTION_SIGNATURE_TYPE"] = signature_type
142142

143143
# Load the source file
144-
spec = importlib.util.spec_from_file_location("main", source)
144+
spec = importlib.util.spec_from_file_location("__main__", source)
145145
source_module = importlib.util.module_from_spec(spec)
146146
sys.path.append(os.path.dirname(os.path.realpath(source)))
147147
spec.loader.exec_module(source_module)

tests/test_functions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,13 @@ def test_lazy_wsgi_app(monkeypatch, target, source, signature_type):
445445
pretend.call(*args, **kwargs),
446446
pretend.call(*args, **kwargs),
447447
]
448+
449+
450+
def test_class_in_main_is_in_right_module():
451+
source = TEST_FUNCTIONS_DIR / "module_is_correct" / "main.py"
452+
target = "function"
453+
454+
client = create_app(target, source).test_client()
455+
resp = client.get("/")
456+
457+
assert resp.status_code == 200
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import typing
16+
17+
18+
class TestClass:
19+
pass
20+
21+
22+
def function(request):
23+
# Ensure that the module for any object in this file is set correctly
24+
assert TestClass.__mro__[0].__module__ == "__main__"
25+
26+
# Ensure that calling `get_type_hints` on an object in this file succeeds
27+
assert typing.get_type_hints(TestClass) == {}
28+
29+
return "OK"

0 commit comments

Comments
 (0)