{"id":6941,"library":"urlobject","title":"URLObject","description":"URLObject is a utility class for manipulating URLs in Python. The latest version, 3.0.0, aims for a clearer API by focusing on explicit method names rather than relying heavily on operator overrides, building upon its predecessors. It provides an immutable object-oriented interface for parsing, constructing, and modifying URLs and their components. It is actively maintained with a recent release.","status":"active","version":"3.0.0","language":"en","source_language":"en","source_url":"http://github.com/zacharyvoase/urlobject","tags":["url","uri","manipulation","parsing","immutable"],"install":[{"cmd":"pip install urlobject","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"URLObject","correct":"from urlobject import URLObject"}],"quickstart":{"code":"import os\nfrom urlobject import URLObject\n\n# Create a URLObject from a string\nurl = URLObject(\"https://github.com/zacharyvoase/urlobject?topic=python&q=docs#api-reference\")\nprint(f\"Original URL: {url}\")\n\n# URLObject instances are immutable; methods return new objects\nmodified_url = (\n    url.with_scheme('http')\n    .with_hostname('example.com')\n    .add_query_param('lang', 'en')\n    .without_fragment()\n)\nprint(f\"Modified URL: {modified_url}\")\n\n# Access individual components\nprint(f\"Scheme: {modified_url.scheme}\")\nprint(f\"Hostname: {modified_url.hostname}\")\nprint(f\"Path: {modified_url.path}\")\nprint(f\"Query params: {modified_url.query_list_multidict()}\")\n\n# Example with authentication (use environment variables for sensitive data)\nauth_url = URLObject(\"https://api.example.com/data\").with_auth(\n    os.environ.get('API_USERNAME', 'your_username'),\n    os.environ.get('API_PASSWORD', 'your_password')\n)\nprint(f\"URL with auth (sensitive parts hidden): {auth_url.without_auth()}\")","lang":"python","description":"Demonstrates creating a URLObject, immutably modifying its components, accessing properties, and an example of handling authentication details using environment variables."},"warnings":[{"fix":"Review your code and replace operator overloads with explicit method calls as described in the library's documentation for version 3.x. For example, use `url.add_path('segment')` instead of `url + 'segment'`.","message":"Major API changes occurred between `urlobject` 2.x and 3.x. Version 3.x shifts focus to 'proper method names over operator overrides'. Code relying on operator overloading (e.g., `url + '/path'`) from older versions will break and needs to be migrated to explicit methods like `.add_path()`, `.with_query()`, etc.","severity":"breaking","affected_versions":"<3.0.0"},{"fix":"Always assign the return value of modification methods to a variable: `my_new_url = original_url.with_scheme('https')`.","message":"URLObject instances are immutable. All methods that 'modify' a URLObject (e.g., `with_scheme()`, `add_query_param()`, `without_fragment()`) return a *new* URLObject instance with the applied changes, leaving the original object untouched. Forgetting to assign the result of these methods will lead to no changes being observed.","severity":"gotcha","affected_versions":"All"},{"fix":"Store sensitive credentials in environment variables and access them using `os.environ.get('YOUR_ENV_VAR', 'default_value')` when constructing URLs with authentication.","message":"When handling sensitive information like API keys or passwords in URLs, avoid hardcoding them directly in your code. Using `os.environ.get()` is a safer practice to retrieve credentials from environment variables.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}