Repository: aio-libs/yarl

Issue:
The port is required to be an int
### Is your feature request related to a problem?

```
File "/home/vscode/.local/ha-venv/lib/python3.12/site-packages/p1monitor/p1monitor.py", line 116, in smartmeter
    data = await self._request(
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/ha-venv/lib/python3.12/site-packages/p1monitor/p1monitor.py", line 59, in _request
    url = URL.build(
          ^^^^^^^^^^
  File "/home/vscode/.local/ha-venv/lib/python3.12/site-packages/yarl/_url.py", line 352, in build
    raise TypeError("The port is required to be int.")
```

https://github.com/home-assistant/core/issues/130074

### Describe the solution you'd like

It would be nice to have the details of the incorrect type.
For example `The port is required to be int, got float.`

### Describe alternatives you've considered

-

### Additional context

_No response_

### Code of Conduct

- [X] I agree to follow the aio-libs Code of Conduct

Discussion:
codspeed-hq[bot]: ## [CodSpeed Performance Report](https://codspeed.io/aio-libs/yarl/branches/Cycloctane:show_port_type_detail)

### Merging #1414 will **not alter performance**

<sub>Comparing <code>Cycloctane:show_port_type_detail</code> (87fe2ba) with <code>master</code> (d6a65eb)</sub>



### Summary

`✅ 83` untouched benchmarks





codecov[bot]: ## [Codecov](https://app.codecov.io/gh/aio-libs/yarl/pull/1414?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 96.06%. Comparing base [(`d6a65eb`)](https://app.codecov.io/gh/aio-libs/yarl/commit/d6a65ebad5d5f0866446c4a6670ac485a7f0e07f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) to head [(`2e3aff6`)](https://app.codecov.io/gh/aio-libs/yarl/commit/2e3aff63e732479c2f16d496edf22dcabd5b921c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs).

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##           master    #1414   +/-   ##
=======================================
  Coverage   96.06%   96.06%           
=======================================
  Files          31       31           
  Lines        5765     5765           
  Branches      344      344           
=======================================
  Hits         5538     5538           
  Misses        201      201           
  Partials       26       26           
```

| [Flag](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | Coverage Δ | |
|---|---|---|
| [CI-GHA](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `96.06% <100.00%> (ø)` | |
| [MyPy](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `48.68% <100.00%> (ø)` | |
| [OS-Linux](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.55% <100.00%> (ø)` | |
| [OS-Windows](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.61% <100.00%> (ø)` | |
| [OS-macOS](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.30% <100.00%> (ø)` | |
| [Py-3.10.11](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.28% <100.00%> (ø)` | |
| [Py-3.10.15](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.51% <100.00%> (ø)` | |
| [Py-3.11.10](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.51% <100.00%> (ø)` | |
| [Py-3.11.9](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.28% <100.00%> (ø)` | |
| [Py-3.12.7](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.51% <100.00%> (ø)` | |
| [Py-3.13.0](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.51% <100.00%> (ø)` | |
| [Py-3.9.13](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.23% <100.00%> (ø)` | |
| [Py-3.9.20](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.47% <100.00%> (ø)` | |
| [Py-pypy7.3.16](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.53% <100.00%> (ø)` | |
| [Py-pypy7.3.17](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `?` | |
| [VM-macos-latest](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.30% <100.00%> (ø)` | |
| [VM-ubuntu-latest](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.55% <100.00%> (ø)` | |
| [VM-windows-latest](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.61% <100.00%> (ø)` | |
| [pytest](https://app.codecov.io/gh/aio-libs/yarl/pull/1414/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs) | `99.55% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs#carryforward-flags-in-the-pull-request-comment) to find out more.

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/aio-libs/yarl/pull/1414?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aio-libs).


Patch:
diff --git a/CHANGES/1414.feature.rst b/CHANGES/1414.feature.rst
new file mode 100644
index 0000000..e34d624
--- /dev/null
+++ b/CHANGES/1414.feature.rst
@@ -0,0 +1,2 @@
+Make error messages include details of incorrect type when ``port`` is not int in :py:meth:`~yarl.URL.build`.
+-- by :user:`Cycloctane`.
diff --git a/yarl/_url.py b/yarl/_url.py
index b69149b..6e8f02e 100644
--- a/yarl/_url.py
+++ b/yarl/_url.py
@@ -349,7 +349,7 @@ class URL:
                 'Can\'t mix "authority" with "user", "password", "host" or "port".'
             )
         if port is not None and not isinstance(port, int):
-            raise TypeError("The port is required to be int.")
+            raise TypeError(f"The port is required to be int, got {type(port)!r}.")
         if port and not host:
             raise ValueError('Can\'t build URL with "port" but without "host".')
         if query and query_string:


Test patch:
diff --git a/tests/test_url_build.py b/tests/test_url_build.py
index a5339dd..43f1383 100644
--- a/tests/test_url_build.py
+++ b/tests/test_url_build.py
@@ -66,7 +66,7 @@ def test_build_with_scheme_and_host():
             id="port-only",
         ),
         pytest.param(
-            "", TypeError, r"^The port is required to be int\.$", id="port-str"
+            "", TypeError, r"^The port is required to be int, got .*\.$", id="port-str"
         ),
     ],
 )