Add optional mypy requirements and update requirements
pyproject.toml: Use optional requirement definitions for mypy to define dependencies on lxml and pip. The optional requirement for pip for downloading typing extensions has been added with https://github.com/python/mypy/issues/13580.
Update all pinned requirements and fix typing issues.