from typing import Optional
import re


def get_upgrade_instructions(pr_description: str) -> Optional[str]:
    """Parse a section labeled "Upgrade instructions" from the PR description.

    This function looks for a section in the PR description that is enclosed in
    the HTML-comments `<!-- UPGRADE INSTRUCTIONS -->`. For example:

    ```md
    ### Upgrade instructions

    <!-- UPGRADE INSTRUCTIONS -->
    - Add the option `Evolution.InitialTime` to evolution input files. Set it
      to the value `0.` to keep the behavior the same as before.
    <!-- UPGRADE INSTRUCTIONS -->
    ```
    """
    FENCE_PATTERN = '<!-- UPGRADE INSTRUCTIONS -->'
    match = re.search(FENCE_PATTERN + '(.*)' + FENCE_PATTERN,
                      pr_description,
                      flags=re.DOTALL)
    if match is None:
        return None
    match = match.group(1).strip()
    if not match or match.isspace():
        return None
    return match