Skip to content

Commit 2dee255

Browse files
authored
SDK - Fix SDK on Python 3.8 (#3126)
* SDK - Fix SDK on Python 3.8 Fixes the follwoing error: "TypeError: code() takes at least 14 arguments (13 given)". The cause of the issue is a breaking change in CodeType constructor in Python 3.8. https://bugs.python.org/issue37221 This should have been fixed by python/cpython#13959 and python/cpython#14505, but the code still fails. * Simplified the replace call
1 parent 54d6e72 commit 2dee255

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

sdk/python/kfp/components/_dynamic.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Any, Callable, Mapping, Sequence
15+
import sys
1616
import types
17+
from typing import Any, Callable, Mapping, Sequence
1718
from inspect import Parameter, Signature
1819

1920

@@ -44,21 +45,31 @@ def pass_locals():
4445
mod_co_filename = code.co_filename
4546
mod_co_firstlineno = code.co_firstlineno
4647

47-
modified_code = types.CodeType(
48-
mod_co_argcount,
49-
code.co_kwonlyargcount,
50-
mod_co_nlocals,
51-
code.co_stacksize,
52-
code.co_flags,
53-
code.co_code,
54-
code.co_consts,
55-
code.co_names,
56-
mod_co_varnames,
57-
mod_co_filename,
58-
mod_co_name,
59-
mod_co_firstlineno,
60-
code.co_lnotab
61-
)
48+
if sys.version_info >= (3, 8):
49+
modified_code = code.replace(
50+
co_argcount=mod_co_argcount,
51+
co_nlocals=mod_co_nlocals,
52+
co_varnames=mod_co_varnames,
53+
co_filename=mod_co_filename,
54+
co_name=mod_co_name,
55+
co_firstlineno=mod_co_firstlineno,
56+
)
57+
else:
58+
modified_code = types.CodeType(
59+
mod_co_argcount,
60+
code.co_kwonlyargcount,
61+
mod_co_nlocals,
62+
code.co_stacksize,
63+
code.co_flags,
64+
code.co_code,
65+
code.co_consts,
66+
code.co_names,
67+
mod_co_varnames,
68+
mod_co_filename,
69+
mod_co_name,
70+
mod_co_firstlineno,
71+
code.co_lnotab
72+
)
6273

6374
default_arg_values = tuple( p.default for p in parameters if p.default != Parameter.empty ) #!argdefs "starts from the right"/"is right-aligned"
6475
modified_func = types.FunctionType(modified_code, {'dict_func': func, 'locals': locals}, name=func_name, argdefs=default_arg_values)

0 commit comments

Comments
 (0)