@graphql-codegen/python-common
Version:
Shared Python-specific helpers (reserved keywords, scalar mapping, identifier naming) for @graphql-codegen/python and @graphql-codegen/python-operations.
59 lines (58 loc) • 1.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PY_IDENT_RE = exports.PYTHON_KEYWORDS = void 0;
exports.isValidClassAttr = isValidClassAttr;
/**
* Python reserved keywords (from the CPython `keyword` module, incl. the soft
* keywords `match`/`case`). Names matching one of these get a trailing `_`
* wherever they'd otherwise be used as a Python identifier.
*/
exports.PYTHON_KEYWORDS = new Set([
'False',
'None',
'True',
'and',
'as',
'assert',
'async',
'await',
'break',
'class',
'continue',
'def',
'del',
'elif',
'else',
'except',
'finally',
'for',
'from',
'global',
'if',
'import',
'in',
'is',
'lambda',
'nonlocal',
'not',
'or',
'pass',
'raise',
'return',
'try',
'while',
'with',
'yield',
'match',
'case',
]);
/** A valid Python identifier (matches the GraphQL Name grammar plus keywords). */
exports.PY_IDENT_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
/**
* Whether `name` can be used as a plain Python class/dict attribute: it must
* be a valid identifier AND not a reserved keyword. When it cannot, the
* containing `TypedDict` must be emitted with functional syntax.
*/
function isValidClassAttr(name) {
return exports.PY_IDENT_RE.test(name) && !exports.PYTHON_KEYWORDS.has(name);
}