Try:
(r'^testapi/(?P<type>type1|type2|type3)/(?P<subtype>subtype\d+|)/?$', my_handler)
>> testapi/type2/
>> [('type2', '')]
>> testapi/type2/subtype1/
>> [('type2', 'subtype1')]
>> testapi/type2/subtype1
>> [('type2', 'subtype1')]
(?P<subtype>subtype\d+|)
will either capture subtype<number>
or <empty string>
.
If you want to make your regex more flexible, you can substitute following:
(?P<subtype>subtype\d+|)
-->(?P<subtype>\w+|)
(?P<type>type1|type2|type3)
-->(?P<type>\w+)
These replacement will not require your url to include subtype and type and accept any string.