Best Python code snippet using playwright-python
documentation_provider.py
Source:documentation_provider.py  
...229    ) -> None:230        if "(arg=)" in fqname or "(pageFunction=)" in fqname:231            return232        code_type = self.serialize_python_type(value)233        doc_type = self.serialize_doc_type(doc_value["type"], direction)234        if not doc_value["required"]:235            doc_type = self.make_optional(doc_type)236        if doc_type != code_type:237            self.errors.add(238                f"Parameter type mismatch in {fqname}: documented as {doc_type}, code has {code_type}"239            )240    def serialize_python_type(self, value: Any) -> str:241        str_value = str(value)242        if isinstance(value, list):243            return f"[{', '.join(list(map(lambda a: self.serialize_python_type(a), value)))}]"244        if str_value == "<class 'playwright._impl._types.Error'>":245            return "Error"246        match = re.match(r"^<class '((?:pathlib\.)?\w+)'>$", str_value)247        if match:248            return match.group(1)249        match = re.match(250            r"playwright._impl._event_context_manager.EventContextManagerImpl\[playwright._impl.[^.]+.(.*)\]",251            str_value,252        )253        if match:254            return "EventContextManager[" + match.group(1) + "]"255        match = re.match(r"^<class 'playwright\._impl\.[\w_]+\.([^']+)'>$", str_value)256        if (257            match258            and "_api_structures" not in str_value259            and "_api_types" not in str_value260        ):261            if match.group(1) == "EventContextManagerImpl":262                return "EventContextManager"263            return match.group(1)264        match = re.match(r"^typing\.(\w+)$", str_value)265        if match:266            return match.group(1)267        origin = get_origin(value)268        args = get_args(value)269        hints = None270        try:271            hints = get_type_hints(value)272        except Exception:273            pass274        if hints:275            signature: List[str] = []276            for [name, value] in hints.items():277                signature.append(f"{name}: {self.serialize_python_type(value)}")278            return f"{{{', '.join(signature)}}}"279        if origin == Union:280            args = get_args(value)281            if len(args) == 2 and str(args[1]) == "<class 'NoneType'>":282                return self.make_optional(self.serialize_python_type(args[0]))283            ll = list(map(lambda a: self.serialize_python_type(a), args))284            ll.sort(key=lambda item: "}" if item == "NoneType" else item)285            return f"Union[{', '.join(ll)}]"286        if str(origin) == "<class 'dict'>":287            args = get_args(value)288            return f"Dict[{', '.join(list(map(lambda a: self.serialize_python_type(a), args)))}]"289        if str(origin) == "<class 'list'>":290            args = get_args(value)291            return f"List[{', '.join(list(map(lambda a: self.serialize_python_type(a), args)))}]"292        if str(origin) == "<class 'collections.abc.Callable'>":293            args = get_args(value)294            return f"Callable[{', '.join(list(map(lambda a: self.serialize_python_type(a), args)))}]"295        if str(origin) == "typing.Literal":296            args = get_args(value)297            if len(args) == 1:298                return '"' + self.serialize_python_type(args[0]) + '"'299            body = ", ".join(300                list(map(lambda a: '"' + self.serialize_python_type(a) + '"', args))301            )302            return f"Union[{body}]"303        return str_value304    def serialize_doc_type(self, type: Any, direction: str) -> str:305        result = self.inner_serialize_doc_type(type, direction)306        return result307    def inner_serialize_doc_type(self, type: Any, direction: str) -> str:308        if type["name"] == "Promise":309            type = type["templates"][0]310        if "union" in type:311            ll = [self.serialize_doc_type(t, direction) for t in type["union"]]312            ll.sort(key=lambda item: "}" if item == "NoneType" else item)313            for i in range(len(ll)):314                if ll[i].startswith("Union["):315                    ll[i] = ll[i][6:-1]316            return f"Union[{', '.join(ll)}]"317        type_name = type["name"]318        if type_name == "path":319            if direction == "in":320                return "Union[pathlib.Path, str]"321            else:322                return "pathlib.Path"323        if type_name == "function" and "args" not in type:324            return "Callable"325        if type_name == "function":326            return_type = "Any"327            if type.get("returnType"):328                return_type = self.serialize_doc_type(type["returnType"], direction)329            return f"Callable[[{', '.join(self.serialize_doc_type(t, direction) for t in type['args'])}], {return_type}]"330        if "templates" in type:331            base = type_name332            if type_name == "Array":333                base = "List"334            if type_name == "Object" or type_name == "Map":335                base = "Dict"336            return f"{base}[{', '.join(self.serialize_doc_type(t, direction) for t in type['templates'])}]"337        if type_name == "Object" and "properties" in type:338            items = []339            for p in type["properties"]:340                items.append(341                    (p["name"])342                    + ": "343                    + (344                        self.serialize_doc_type(p["type"], direction)345                        if p["required"]346                        else self.make_optional(347                            self.serialize_doc_type(p["type"], direction)348                        )349                    )350                )351            return f"{{{', '.join(items)}}}"352        if type_name == "boolean":353            return "bool"354        if type_name == "string":355            return "str"356        if type_name == "any" or type_name == "Serializable":357            return "Any"358        if type_name == "Object":359            return "Dict"360        if type_name == "Function":361            return "Callable"...LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!
