Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion playwright/_impl/protocol/validator_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ def validator(arg: Any, path: str, context: ValidatorContext) -> Any:
return _NULL if keeps_null and context.in_array else arg
return v(arg, path, context)

# Python: mark optional validators so t_object can skip absent fields.
setattr(validator, "_is_optional", True)
return validator


Expand All @@ -200,11 +202,16 @@ def validator(arg: Any, path: str, context: ValidatorContext) -> Any:


def t_object(s: Dict[str, Validator]) -> Validator:
# Python: cache the schema and optional flags to avoid calls for absent fields.
fields = [(key, v, getattr(v, "_is_optional", False)) for key, v in s.items()]

def validator(arg: Any, path: str, context: ValidatorContext) -> Any:
if not isinstance(arg, dict):
raise ValidationError(f"{path}: expected object, got {_typeof(arg)}")
result: Dict[str, Any] = {}
for key, v in s.items():
for key, v, optional in fields:
if optional and key not in arg:
continue
value = v(arg.get(key), path + "." + key if path else key, context)
if value is _NULL:
# Python: a key that is there with None is null; a key that is not
Expand Down
59 changes: 59 additions & 0 deletions tests/sync/python/test_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Python only: tests of behaviors that no upstream test covers."""

import sys
from types import FrameType
from typing import Any

from playwright._impl.protocol.validator_primitives import t_any, t_optional
from playwright.sync_api import Page


def test_evaluate_should_not_validate_absent_optional_fields(page: Page) -> None:
# Regression test for https://github.com/microsoft/playwright-python/issues/3362.
optional_code = getattr(t_optional(t_any), "__code__")
absent_calls = 0

def profile(frame: FrameType, event: str, arg: Any) -> None:
nonlocal absent_calls
if event == "call" and frame.f_code is optional_code:
path = frame.f_locals["path"]
if path.startswith("value.") and frame.f_locals["arg"] is None:
absent_calls += 1

previous_profile = sys.getprofile()
sys.setprofile(profile)
try:
result = page.evaluate(
"""Array.from({length: 200}, (_, i) => ({
id: i, name: 'item ' + i, price: i * 1.5,
url: 'https://example.com/' + i, tags: ['a', 'b']
}))"""
)
finally:
sys.setprofile(previous_profile)

assert result == [
{
"id": i,
"name": f"item {i}",
"price": i * 1.5,
"url": f"https://example.com/{i}",
"tags": ["a", "b"],
}
for i in range(200)
]
assert absent_calls == 0
Loading