Skip to content

Commit

Permalink
SDK - Fix SDK on Python 3.8
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ark-kun committed Feb 19, 2020
1 parent 24d77ed commit d161b00
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions sdk/python/kfp/components/_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Callable, Mapping, Sequence
import sys
import types
from typing import Any, Callable, Mapping, Sequence
from inspect import Parameter, Signature


Expand Down Expand Up @@ -44,21 +45,31 @@ def pass_locals():
mod_co_filename = code.co_filename
mod_co_firstlineno = code.co_firstlineno

modified_code = types.CodeType(
mod_co_argcount,
code.co_kwonlyargcount,
mod_co_nlocals,
code.co_stacksize,
code.co_flags,
code.co_code,
code.co_consts,
code.co_names,
mod_co_varnames,
mod_co_filename,
mod_co_name,
mod_co_firstlineno,
code.co_lnotab
)
if sys.version_info >= (3, 8):
modified_code = (code
.replace(co_argcount=mod_co_argcount)
.replace(co_nlocals=mod_co_nlocals)
.replace(co_varnames=mod_co_varnames)
.replace(co_filename=mod_co_filename)
.replace(co_name=mod_co_name)
.replace(co_firstlineno=mod_co_firstlineno)
)
else:
modified_code = types.CodeType(
mod_co_argcount,
code.co_kwonlyargcount,
mod_co_nlocals,
code.co_stacksize,
code.co_flags,
code.co_code,
code.co_consts,
code.co_names,
mod_co_varnames,
mod_co_filename,
mod_co_name,
mod_co_firstlineno,
code.co_lnotab
)

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

0 comments on commit d161b00

Please sign in to comment.