diff --git a/py/argcheck.c b/py/argcheck.c
new file mode 100644
index 0000000000000000000000000000000000000000..5a0bb8411653e8b35496f8499ba97b5aa1697aee
--- /dev/null
+++ b/py/argcheck.c
@@ -0,0 +1,78 @@
+#include <stdlib.h>
+#include <assert.h>
+
+#include "nlr.h"
+#include "misc.h"
+#include "mpconfig.h"
+#include "qstr.h"
+#include "obj.h"
+#include "runtime.h"
+
+void mp_arg_check_num(uint n_args, uint n_kw, uint n_args_min, uint n_args_max, bool takes_kw) {
+    // TODO maybe take the function name as an argument so we can print nicer error messages
+
+    if (n_kw && !takes_kw) {
+        nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
+    }
+
+    if (n_args_min == n_args_max) {
+        if (n_args != n_args_min) {
+            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
+                                                    "function takes %d positional arguments but %d were given",
+                                                    n_args_min, n_args));
+        }
+    } else {
+        if (n_args < n_args_min) {
+            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
+                                                    "function missing %d required positional arguments",
+                                                    n_args_min - n_args));
+        } else if (n_args > n_args_max) {
+            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
+                                                    "function expected at most %d arguments, got %d",
+                                                    n_args_max, n_args));
+        }
+    }
+}
+
+void mp_arg_parse_all(uint n_pos, const mp_obj_t *pos, mp_map_t *kws, uint n_allowed, const mp_arg_parse_t *allowed, mp_arg_parse_val_t *out_vals) {
+    uint pos_found = 0, kws_found = 0;
+    for (uint i = 0; i < n_allowed; i++) {
+        mp_obj_t given_arg;
+        if (i < n_pos) {
+            if (allowed[i].flags & MP_ARG_PARSE_KW_ONLY) {
+                nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' argument must be given by a keyword", qstr_str(allowed[i].qstr)));
+            }
+            pos_found++;
+            given_arg = pos[i];
+        } else {
+            mp_map_elem_t *kw = mp_map_lookup(kws, MP_OBJ_NEW_QSTR(allowed[i].qstr), MP_MAP_LOOKUP);
+            if (kw == NULL) {
+                if (allowed[i].flags & MP_ARG_PARSE_REQUIRED) {
+                    nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' argument required", qstr_str(allowed[i].qstr)));
+                }
+                out_vals[i] = allowed[i].defval;
+                continue;
+            } else {
+                kws_found++;
+                given_arg = kw->value;
+            }
+        }
+        if (allowed[i].flags == MP_ARG_PARSE_BOOL) {
+            out_vals[i].u_bool = mp_obj_is_true(given_arg);
+        } else if (allowed[i].flags == MP_ARG_PARSE_INT) {
+            out_vals[i].u_int = mp_obj_get_int(given_arg);
+        } else if (allowed[i].flags == MP_ARG_PARSE_OBJ) {
+            out_vals[i].u_obj = given_arg;
+        } else {
+            assert(0);
+        }
+    }
+    if (pos_found < n_pos) {
+        // TODO better error message
+        nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "extra positional arguments given"));
+    }
+    if (kws_found < kws->used) {
+        // TODO better error message
+        nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "extra keyword arguments given"));
+    }
+}
diff --git a/py/objarray.c b/py/objarray.c
index 2255e29d7be4977b403b4cdb291517c30cb2ed37..cf8b1ed4eb2d2e4be2bea23c6a039e9c12b73816 100644
--- a/py/objarray.c
+++ b/py/objarray.c
@@ -75,7 +75,7 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
 }
 
 STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
-    mp_check_nargs(n_args, 1, 2, n_kw, false);
+    mp_arg_check_num(n_args, n_kw, 1, 2, false);
 
     // get typecode
     uint l;
@@ -91,7 +91,7 @@ STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
 }
 
 STATIC mp_obj_t bytearray_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
-    mp_check_nargs(n_args, 0, 1, n_kw, false);
+    mp_arg_check_num(n_args, n_kw, 0, 1, false);
 
     if (n_args == 0) {
         // no args: construct an empty bytearray
diff --git a/py/objfun.c b/py/objfun.c
index ab6326057f7f0f0009f670b06fef19df88e57ad8..c7144f30788f0b8a41b2a63bc5dba040cd020642 100644
--- a/py/objfun.c
+++ b/py/objfun.c
@@ -25,36 +25,6 @@
 
 // mp_obj_fun_native_t defined in obj.h
 
-STATIC void check_nargs(mp_obj_fun_native_t *self, int n_args, int n_kw) {
-    mp_check_nargs(n_args, self->n_args_min, self->n_args_max, n_kw, self->is_kw);
-}
-
-void mp_check_nargs(int n_args, machine_uint_t n_args_min, machine_uint_t n_args_max, int n_kw, bool is_kw) {
-    // TODO maybe take the function name as an argument so we can print nicer error messages
-
-    if (n_kw && !is_kw) {
-        nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
-    }
-
-    if (n_args_min == n_args_max) {
-        if (n_args != n_args_min) {
-            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
-                                                    "function takes %d positional arguments but %d were given",
-                                                    n_args_min, n_args));
-        }
-    } else {
-        if (n_args < n_args_min) {
-            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
-                                                    "function missing %d required positional arguments",
-                                                    n_args_min - n_args));
-        } else if (n_args > n_args_max) {
-            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
-                                                    "function expected at most %d arguments, got %d",
-                                                    n_args_max, n_args));
-        }
-    }
-}
-
 STATIC mp_obj_t fun_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
     switch (op) {
         case MP_BINARY_OP_EQUAL:
@@ -70,7 +40,7 @@ STATIC mp_obj_t fun_native_call(mp_obj_t self_in, uint n_args, uint n_kw, const
     mp_obj_fun_native_t *self = self_in;
 
     // check number of arguments
-    check_nargs(self, n_args, n_kw);
+    mp_arg_check_num(n_args, n_kw, self->n_args_min, self->n_args_max, self->is_kw);
 
     if (self->is_kw) {
         // function allows keywords
diff --git a/py/objrange.c b/py/objrange.c
index 7815a5951e3552742f790b65823bfb3c225719a3..8c0eadd32e32fcda87be833a4c77b28f2c6e90ba 100644
--- a/py/objrange.c
+++ b/py/objrange.c
@@ -57,7 +57,7 @@ typedef struct _mp_obj_range_t {
 } mp_obj_range_t;
 
 STATIC mp_obj_t range_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
-    mp_check_nargs(n_args, 1, 3, n_kw, false);
+    mp_arg_check_num(n_args, n_kw, 1, 3, false);
 
     mp_obj_range_t *o = m_new_obj(mp_obj_range_t);
     o->base.type = &mp_type_range;
diff --git a/py/py.mk b/py/py.mk
index dd6ddd0b35cca7c3906a7536f8a7af5fb21b2358..5b0dd3d3a8fb04414d551390b428d70199c948e0 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -40,6 +40,7 @@ PY_O_BASENAME = \
 	parsenum.o \
 	emitglue.o \
 	runtime.o \
+	argcheck.o \
 	map.o \
 	obj.o \
 	objarray.o \
diff --git a/py/runtime.h b/py/runtime.h
index 3297a21a344e51d3e6e37d458b1572de43361d63..5ce4bbc5e101d5c6b733855dfb2636f7f3081e65 100644
--- a/py/runtime.h
+++ b/py/runtime.h
@@ -4,10 +4,32 @@ typedef enum {
     MP_VM_RETURN_EXCEPTION,
 } mp_vm_return_kind_t;
 
+typedef enum {
+    MP_ARG_PARSE_BOOL      = 0x001,
+    MP_ARG_PARSE_INT       = 0x002,
+    MP_ARG_PARSE_OBJ       = 0x003,
+    MP_ARG_PARSE_KIND_MASK = 0x0ff,
+    MP_ARG_PARSE_REQUIRED  = 0x100,
+    MP_ARG_PARSE_KW_ONLY   = 0x200,
+} mp_arg_parse_flag_t;
+
+typedef union _mp_arg_parse_val_t {
+    bool u_bool;
+    machine_int_t u_int;
+    mp_obj_t u_obj;
+} mp_arg_parse_val_t;
+
+typedef struct _mp_arg_parse_t {
+    qstr qstr;
+    machine_uint_t flags;
+    mp_arg_parse_val_t defval;
+} mp_arg_parse_t;
+
 void mp_init(void);
 void mp_deinit(void);
 
-void mp_check_nargs(int n_args, machine_uint_t n_args_min, machine_uint_t n_args_max, int n_kw, bool is_kw);
+void mp_arg_check_num(uint n_args, uint n_kw, uint n_args_min, uint n_args_max, bool takes_kw);
+void mp_arg_parse_all(uint n_pos, const mp_obj_t *pos, mp_map_t *kws, uint n_allowed, const mp_arg_parse_t *allowed, mp_arg_parse_val_t *out_vals);
 
 mp_obj_dict_t *mp_locals_get(void);
 void mp_locals_set(mp_obj_dict_t *d);