From b8c589c525f1b5ed7a7a07ab4c308ec8150222a0 Mon Sep 17 00:00:00 2001 From: "microsoft-playwright-automation[bot]" <203992400+microsoft-playwright-automation[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:03:52 +0000 Subject: [PATCH] fix(protocol): skip absent optional fields during validation Fixes: https://github.com/microsoft/playwright-python/issues/3362 --- .../_impl/protocol/validator_primitives.py | 9 ++- tests/sync/python/test_page.py | 59 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/sync/python/test_page.py diff --git a/playwright/_impl/protocol/validator_primitives.py b/playwright/_impl/protocol/validator_primitives.py index c3403abbc..0613125ee 100644 --- a/playwright/_impl/protocol/validator_primitives.py +++ b/playwright/_impl/protocol/validator_primitives.py @@ -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 @@ -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 diff --git a/tests/sync/python/test_page.py b/tests/sync/python/test_page.py new file mode 100644 index 000000000..c55e1c8a2 --- /dev/null +++ b/tests/sync/python/test_page.py @@ -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