Skip to content

Commit 56da5c4

Browse files
afshinCarreau
andauthored
Switch schema version type to str (#104)
* Switch schema `version` type to `str` Type coerce `version` to be a string * lints * pass interoogate --------- Co-authored-by: M Bussonnier <[email protected]>
1 parent f613554 commit 56da5c4

19 files changed

+62
-40
lines changed

docs/demo/demo-notebook.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
},
4848
{
4949
"cell_type": "code",
50-
"execution_count": 2,
50+
"execution_count": null,
5151
"metadata": {},
5252
"outputs": [
5353
{
@@ -72,7 +72,7 @@
7272
"source": [
7373
"schema = \"\"\"\n",
7474
"$id: http://myapplication.org/example-event\n",
75-
"version: 1\n",
75+
"version: \"1\"\n",
7676
"title: Example Event\n",
7777
"description: An interesting event to collect\n",
7878
"properties:\n",

docs/user_guide/application.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Register an event schema with the logger.
2222
```python
2323
schema = """
2424
$id: http://myapplication.org/my-method
25-
version: 1
25+
version: "1"
2626
title: My Method Executed
2727
description: My method was executed one time.
2828
properties:

docs/user_guide/defining-schema.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Beyond these required items, any valid JSON should be possible. Here is a simple
2727

2828
```yaml
2929
$id: https://event.jupyter.org/example-event
30-
version: 1
30+
version: "1"
3131
title: My Event
3232
description: |
3333
Some information about my event
@@ -67,7 +67,7 @@ The output will look like this, if it passes:
6767
6868
{
6969
"$id": "http://event.jupyter.org/test",
70-
"version": 1,
70+
"version": "1",
7171
"title": "Simple Test Schema",
7272
"description": "A simple schema for testing\n",
7373
"type": "object",
@@ -92,7 +92,7 @@ or this if fails:
9292
9393
{
9494
"$id": "http://event.jupyter.org/test",
95-
"version": 1,
95+
"version": "1",
9696
"title": "Simple Test Schema",
9797
"description": "A simple schema for testing\n",
9898
"type": "object",

docs/user_guide/event-schemas.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ from jupyter_events.logger import EventLogger
1313

1414
schema = """
1515
$id: http://myapplication.org/example-event
16-
version: 1
16+
version: "1"
1717
title: Example Event
1818
description: An interesting event to collect
1919
properties:
@@ -38,7 +38,7 @@ print(logger.schemas)
3838
Validator class: Draft7Validator
3939
Schema: {
4040
"$id": "myapplication.org/example-event",
41-
"version": 1,
41+
""version": 1",
4242
"title": "Example Event",
4343
"description": "An interesting event to collect",
4444
"properties": {

docs/user_guide/first-event.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ To begin emitting events from a Python application, you need to tell the `EventL
1515
```python
1616
schema = """
1717
$id: http://myapplication.org/example-event
18-
version: 1
18+
version: "1"
1919
title: Example Event
2020
description: An interesting event to collect
2121
properties:

jupyter_events/schemas/event-core-schema.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$schema: http://json-schema.org/draft-07/schema
22
$id: http://event.jupyter.org/event-schema
3-
version: 1
3+
version: "1"
44
title: Event Schema
55
description: |
66
A schema for validating any Jupyter Event.
@@ -12,7 +12,7 @@ properties:
1212
const: 1
1313
__schema_version__:
1414
title: Schema Version
15-
type: integer
15+
type: string
1616
__schema__:
1717
title: Schema ID
1818
type: string

jupyter_events/schemas/event-metaschema.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
$schema: http://json-schema.org/draft-07/schema
22
$id: http://event.jupyter.org/event-metaschema
3-
version: 1
3+
version: "1"
44
title: Event Metaschema
55
description: |
66
A meta schema for validating that all registered Jupyter Event
77
schemas are appropriately defined.
88
type: object
99
properties:
1010
version:
11-
type: integer
11+
type: string
1212
title:
1313
type: string
1414
description:

jupyter_events/schemas/property-metaschema.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$schema: http://json-schema.org/draft-07/schema
22
$id: http://event.jupyter.org/property-metaschema
3-
version: 1
3+
version: "1"
44
title: Property Metaschema
55
description: |
66
A metaschema for validating properties within

jupyter_events/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Various utilities
3+
"""
4+
from __future__ import annotations
5+
6+
7+
class JupyterEventsVersionWarning(UserWarning):
8+
"""Emitted when an event schema version is an `int` when it should be `str`."""

jupyter_events/validators.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import annotations
33

44
import pathlib
5+
import warnings
56
from typing import Any
67

78
import jsonschema
@@ -10,6 +11,7 @@
1011
from referencing.jsonschema import DRAFT7
1112

1213
from . import yaml
14+
from .utils import JupyterEventsVersionWarning
1315

1416
draft7_format_checker = (
1517
Draft7Validator.FORMAT_CHECKER
@@ -57,6 +59,17 @@
5759
def validate_schema(schema: dict[str, Any]) -> None:
5860
"""Validate a schema dict."""
5961
try:
62+
# If the `version` attribute is an integer, coerce to string.
63+
# TODO: remove this in a future version.
64+
if "version" in schema and isinstance(schema["version"], int):
65+
schema["version"] = str(schema["version"])
66+
msg = (
67+
"The `version` property of an event schema must be a string. "
68+
"It has been type coerced, but in a future version of this "
69+
"library, it will fail to validate. Please update schema: "
70+
f"{schema['$id']}"
71+
)
72+
warnings.warn(JupyterEventsVersionWarning(msg), stacklevel=2)
6073
# Validate the schema against Jupyter Events metaschema.
6174
JUPYTER_EVENTS_SCHEMA_VALIDATOR.validate(schema)
6275
except ValidationError as err:

0 commit comments

Comments
 (0)