Repository: microsoft/playwright-python

Issue:
internal: setup env variables when launching driver
1.18 driver assumes the following environment variables are set:

```
PW_CLI_TARGET_LANG=python
PW_CLI_TARGET_LANG_VERSION=<version>
PW_CLI_DISPLAY_VERSION=CLI_VERSION (of the language)
```

`version` should not be too granular to avoid noise; IMO major + minor seems to be good for Python.


Discussion:

Resolving patch:
diff --git a/playwright/__main__.py b/playwright/__main__.py
--- a/playwright/__main__.py
+++ b/playwright/__main__.py
@@ -12,20 +12,17 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import os
 import subprocess
 import sys
 
-from playwright._impl._driver import compute_driver_executable
-from playwright._repo_version import version
+from playwright._impl._driver import compute_driver_executable, get_driver_env
 
 
 def main() -> None:
     driver_executable = compute_driver_executable()
-    env = os.environ.copy()
-    env["PW_CLI_TARGET_LANG"] = "python"
-    env["PW_CLI_DISPLAY_VERSION"] = version
-    completed_process = subprocess.run([str(driver_executable), *sys.argv[1:]], env=env)
+    completed_process = subprocess.run(
+        [str(driver_executable), *sys.argv[1:]], env=get_driver_env()
+    )
     sys.exit(completed_process.returncode)
 
 
diff --git a/playwright/_impl/_driver.py b/playwright/_impl/_driver.py
--- a/playwright/_impl/_driver.py
+++ b/playwright/_impl/_driver.py
@@ -14,10 +14,12 @@
 
 import asyncio
 import inspect
+import os
 import sys
 from pathlib import Path
 
 import playwright
+from playwright._repo_version import version
 
 
 def compute_driver_executable() -> Path:
@@ -42,3 +44,13 @@ def compute_driver_executable() -> Path:
             # uvloop does not support child watcher
             # see https://github.com/microsoft/playwright-python/issues/582
             pass
+
+
+def get_driver_env() -> dict:
+    env = os.environ.copy()
+    env["PW_CLI_TARGET_LANG"] = "python"
+    env[
+        "PW_CLI_TARGET_LANG_VERSION"
+    ] = f"{sys.version_info.major}.{sys.version_info.minor}"
+    env["PW_CLI_DISPLAY_VERSION"] = version
+    return env
diff --git a/playwright/_impl/_transport.py b/playwright/_impl/_transport.py
--- a/playwright/_impl/_transport.py
+++ b/playwright/_impl/_transport.py
@@ -28,6 +28,7 @@
 from websockets.client import connect as websocket_connect
 
 from playwright._impl._api_types import Error
+from playwright._impl._driver import get_driver_env
 from playwright._impl._helper import ParsedMessagePayload
 
 
@@ -116,7 +117,7 @@ async def connect(self) -> None:
 
         try:
             # For pyinstaller
-            env = os.environ.copy()
+            env = get_driver_env()
             if getattr(sys, "frozen", False):
                 env.setdefault("PLAYWRIGHT_BROWSERS_PATH", "0")
 


Test patch:
diff --git a/tests/async/test_fetch_global.py b/tests/async/test_fetch_global.py
--- a/tests/async/test_fetch_global.py
+++ b/tests/async/test_fetch_global.py
@@ -14,6 +14,7 @@
 
 import asyncio
 import json
+import sys
 from pathlib import Path
 from typing import Any
 
@@ -263,3 +264,16 @@ async def test_should_accept_already_serialized_data_as_bytes_when_content_type_
     body = req.post_body
     assert body == stringified_value
     await request.dispose()
+
+
+async def test_should_contain_default_user_agent(
+    playwright: Playwright, server: Server
+):
+    request = await playwright.request.new_context()
+    [request, _] = await asyncio.gather(
+        server.wait_for_request("/empty.html"),
+        request.get(server.EMPTY_PAGE),
+    )
+    user_agent = request.getHeader("user-agent")
+    assert "python" in user_agent
+    assert f"{sys.version_info.major}.{sys.version_info.minor}" in user_agent